﻿using Entity;
using ServiceCore;
using ServiceCore.Tool.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using ServiceCore.Tool.NPOI;
using Microsoft.AspNetCore.Http;
using System.Web;
using ServiceCore.Tool.Post;
using System.ComponentModel;

namespace Customer.Bussiness_Service
{
    public interface Irepair_service
    {
        /// <summary>
        /// 获取报修类型
        /// </summary>
        /// <returns></returns>
        ResultObjModel get_repair_type();
        /// <summary>
        /// 保存报修类型
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        ResultObjModel save_repair_type(List<repair_type> list);
        /// <summary>
        /// 报修登记
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        ResultObjModel repair_register(repair_register_params model);
        /// <summary>
        /// 获取报修详情结果
        /// </summary>
        /// <param name="repair_id"></param>
        /// <returns></returns>
        ResultObjModel repair_model(string repair_id);
        /// <summary>
        /// 获取报修列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        ResultListModel get_repair_pagelist(Base_List_Params query);
        /// <summary>
        /// 获取登录用户处理的报修列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        ResultListModel get_my_repair_pagelist(search_my_repair_list query);
        /// <summary>
        /// 转技术售后
        /// </summary>
        /// <param name="repair_id"></param>
        /// <param name="technology_uid"></param>
        /// <returns></returns>
        ResultModel give_technology_repair(string repair_id, string technology_uid);
        /// <summary>
        /// 申请审批
        /// </summary>
        /// <param name="repair_id"></param>
        /// <param name="approval_uid"></param>
        /// <param name="repair_approval_des"></param>
        ResultModel application_approval(string repair_id, string approval_uid, string repair_approval_des,
            approval_model? approval_model, string express_model, decimal? freight, decimal? approval_price, string express_no, string[] transfer_screenshot = null);
        /// <summary>
        /// 上传审批图片
        /// </summary>
        /// <param name="repair_id"></param>
        /// <param name="repair_content_pic"></param>
        /// <param name="repair_express_no"></param>
        /// <returns></returns>
        ResultModel upload_approval_pic(string repair_id, string repair_content_pic, string repair_express_no);

        /// <summary>
        /// 根据当前业务等级获取 获取员工列表
        /// </summary>
        /// <param name="lv"></param>
        /// <returns></returns>
        ResultObjModel get_customer_userlist(string lv, approval_model? approval_model);
        /// <summary>
        /// 保存报修状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        ResultModel save_repair(save_repair_params model);
        /// <summary>
        /// 获取我审核的列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        ResultListModel<get_my_approval_pagelist_excel> get_my_approval_pagelist(search_my_approval_list query);

        void down_my_approval_pagelist_for_excel(search_my_approval_list query);
        /// <summary>
        /// 处理审批
        /// </summary>
        /// <param name="repair_id"></param>
        /// <param name="state"></param>
        /// <param name="repair_approval_des"></param>
        /// <returns></returns>
        ResultModel handle_apply(string[] repair_ids, apply_state? state, string repair_approval_des);
        /// <summary>
        /// 修改寄件状态
        /// </summary>
        /// <param name="repair_id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        ResultModel handle_apply_express_state(string repair_id, express_state? state);
        /// <summary>
        /// 查看所有报修信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        ResultListModel admin_get_repair_pagelist(search_my_repair_list query);
        /// <summary>
        ///获取快递单号图片
        /// </summary>
        /// <param name="repair_id"></param>
        /// <returns></returns>
        ResultObjModel get_repair_express(string repair_id);

        /// <summary>
        /// 分页查询统计页
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        ResultListModel<customer_repairForexcelPage> get_repair_pagelist_for_excel(search_my_repair_list_for_excel query);
        void down_repair_pagelist_for_excel(search_my_repair_list_for_excel query);
        /// <summary>
        /// 返回配件处理
        /// </summary>
        /// <param name="Params"></param>
        /// <returns></returns>
        ResultModel reback_data_info(reback_data_info_params Params);
        /// <summary>
        /// 获取审批列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        ResultListModel<get_approval_pagelist_for_Excel> get_approval_pagelist(search_my_approval_list query);
        /// <summary>
        /// 下载审批列表
        /// </summary>
        /// <param name="query"></param>
        void down_approval_pagelist_for_excel(search_my_approval_list query);
        /// <summary>
        /// 提交一次返件审批
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        ResultObjModel submit_return_approval(submit_return_approval_params model);
        /// <summary>
        /// 返件审批
        /// </summary>
        /// <param name="repair_id"></param>
        /// <returns></returns>
        ResultObjModel get_return_approval(string repair_id); 

        /// <summary>
        /// 返件状态审批
        /// </summary>
        /// <param name="repair_return_id"></param>
        /// <param name="state"></param>
        /// <param name="return_approval_des"></param>
        /// <param name="open_add"></param>
        /// <returns></returns>
        ResultModel handle_apply_return_state(string repair_return_id, return_approval_state? state, string return_approval_des, bool? open_add = false);
        /// <summary>
        /// 返回返件列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        ResultListModel<return_approval_item> get_return_approval_list(search_return_approval_list query);
        /// <summary>
        ///  针对系统研发之前的返件进行的补充
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        ResultObjModel submit_return_approval_for_zhanghuan(submit_return_approval_params model);

        /// <summary>
        /// 根据搜索条件获取登录人的报修信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        ResultListModel get_over_insurance_approval_pagelist(search_my_approval_list query);
    }
    public class repair_service : Irepair_service
    {
        private readonly IToole_DI _tool;
        private readonly IRW_Helper _template;
        private readonly INpoi_service _excelservice;
        private readonly IHttpContextAccessor _context;

        public repair_service(IToole_DI Tool, IRW_Helper Template, INpoi_service Excelservice, IHttpContextAccessor Context)
        {
            _tool = Tool;
            _template = Template;
            _excelservice = Excelservice;
            _context = Context;
        }
        /// <summary>
        /// 获取报修类型
        /// </summary>
        /// <returns></returns>
        public ResultObjModel get_repair_type()
        {
            return CoreStartDo.ResultAction(result =>
            {
                result.code = 0;
                result.msg = "success";
                var temp_list = _template.GetStructure_Template();
                var resultdate = from o in temp_list
                                 select new
                                 {
                                     label = o.type_name,
                                     children = from c in o.kid_type_name
                                                select new
                                                {
                                                    label = c
                                                }
                                 };
                //result.data = _template.GetStructure_Template();
                result.data = resultdate;
                return result;
            });
        }
        /// <summary>
        /// 保存报修类型
        /// </summary>
        /// <returns></returns>
        public ResultObjModel save_repair_type(List<repair_type> list)
        {
            return CoreStartDo.ResultAction(result =>
            {
                _template.WriteTemplate(list);
                result.code = 0;
                result.msg = "success";
                return result;
            });
        }
        /// <summary>
        /// 报修登记
        /// </summary>
        /// <returns></returns>
        public ResultObjModel repair_register(repair_register_params model)
        {
            return CoreStartDo.ResultAction(result =>
            {
                #region 数据验证
                if (string.IsNullOrWhiteSpace(model.mchid))
                {
                    result.code = 1;
                    result.msg = "机器ID不能为空";
                    return result;
                }
                if (string.IsNullOrWhiteSpace(model.repair_type) && string.IsNullOrWhiteSpace(model.repair_other))
                {
                    result.code = 1;
                    result.msg = "请选择报修类型";
                    return result;
                }
                if (string.IsNullOrWhiteSpace(model.repair_des))
                {
                    result.code = 1;
                    result.msg = "请填写问题描述";
                    return result;
                }
                if (model.repair_des.Length > 200)
                {
                    result.code = 1;
                    result.msg = "问题描述不得大于200字";
                }
                if (_tool.di_user.USER.role_id == "technology_Customer")
                {
                    result.code = 1;
                    result.msg = "您的角色无法进行报修登记";
                    return result;
                }
                //检测机器是否存在
                Mch mch = _tool.di_db.Get_rk_List<Mch>("select * from Mch where Id=@Id", new { Id = model.mchid }).FirstOrDefault();
                if (mch == null)
                {
                    result.code = 1;
                    result.msg = "机器不存在";
                    return result;
                }
                User user = _tool.di_db.Get_rk_List<User>("select * from [User] where Id=@Id", new { Id = mch.BaseUserId }).FirstOrDefault();
                if (user == null)
                {
                    result.code = 1;
                    result.msg = "用户不存在";
                    return result;
                }
                #endregion
                //开始构造报修记录
                customer_repair new_model = new customer_repair()
                {
                    repair_id = _tool.di_getid.IGet_SnowFlake_Id,
                    addtime = DateTime.Now,
                    user_id = user.Id,
                    user_name = user.Name,
                    user_wx_name = user.wx_name,
                    user_loginname = user.UserName,
                    user_tel = user.Tel,
                    mch_id = mch.Id,
                    mch_sn = mch.SN,
                    mch_name = mch.Name,
                    mch_type = mch.MchType,
                    mch_assurance_sdt = mch.assurance_sdt,
                    repair_type = model.repair_type,
                    repair_kid_type = model.repair_kid_type,
                    repair_other = model.repair_other,
                    repair_des = model.repair_des,
                    repair_time = DateTime.Now,
                    repair_handle_state = repair_handle_state.售后处理中,
                    customer_repair_handle_result = "",
                    technology_repair_handle_result = "",
                    repair_approval_des = "无需审批",
                    customer_uid = _tool.di_user.USER.uid,
                    customer_uname = _tool.di_user.USER.u_name,
                    repair_tel = model.repair_tel,
                    repair_des_pic = Newtonsoft.Json.JsonConvert.SerializeObject(model.repair_des_pic)
                };
                //设置谁能看
                customer_repair_canshow canshow_model = new customer_repair_canshow()
                {
                    repair_id = new_model.repair_id,
                    canshow_id = _tool.di_getid.IGet_SnowFlake_Id,
                    uid = _tool.di_user.USER.uid
                };
                string errormsg = "";
                int tran_rows = _tool.di_db.StartTran((conn, tran) =>
                {
                    int rows = 0;
                    rows += conn.Execute(_tool.di_db.AddBySqlStr<customer_repair>(), new_model, tran);
                    rows += conn.Execute(_tool.di_db.AddBySqlStr<customer_repair_canshow>(), canshow_model, tran);
                    return rows;
                }, ref errormsg);
                if (tran_rows == 2)
                {
                    result.code = 0;
                    result.msg = "保存成功";
                }
                else
                {
                    result.code = 1;
                    result.msg = "保存失败 " + errormsg;
                }
                return result;
            });
        }
        /// <summary>
        ///  获取报修详情信息
        /// </summary>
        /// <returns></returns>
        public ResultObjModel repair_model(string repair_id)
        {
            return CoreStartDo.ResultAction(result =>
            {

                if (string.IsNullOrWhiteSpace(repair_id))
                {
                    result.code = 1;
                    result.msg = "参数错误,报修id未找到";
                    return result;
                }
                //验证当前登录用户是否可见该数据
                var canshow = _tool.di_db.GetList<customer_repair_canshow>("select top 1 * from customer_repair_canshow where uid=@uid and repair_id=@repair_id", new { _tool.di_user.USER.uid, repair_id });
                if (canshow == null)
                {
                    result.code = 1;
                    result.msg = "您不能处理该数据";
                    return result;
                }
                var org_list = _tool.di_db.GetList<customer_repair>("select * from customer_repair where repair_id=@repair_id", new { repair_id }).ToList();
                //数据处理
                var result_model = from o in org_list
                                   select new
                                   {
                                       o.addtime,//报修日期
                                       o.mch_assurance_sdt,//质保开始日期
                                       o.user_name,//用户名字,
                                       o.user_wx_name,//用户微信名,
                                       o.mch_sn,//机器SN,
                                       o.repair_type,//报修类型
                                       o.repair_kid_type, //报修子类型
                                       o.repair_other,//其他报修类型
                                       o.repair_des,//报修描述
                                       o.repair_handle_state,//当前处理状态
                                       repair_handle_state_str = o.repair_handle_state.ToString(),
                                       o.customer_repair_handle_result,//一般售后处理结果,
                                       o.technology_repair_handle_result,//技术售后处理结果,
                                       o.approval_repair_handle_result,//审核处理结果
                                       o.repair_approval_des,//提交审批内容
                                       o.approval_uid,//审批人ID
                                       can_approval = string.IsNullOrWhiteSpace(o.technology_uid) ? false : _tool.di_user.USER.role_id == "Customer" ? false : true,//是否可以进行提交审批等操作,
                                       can_customer = (_tool.di_user.USER.role_id == "Customer" || _tool.di_user.USER.role_id == "all_Customer") ? new repair_handle_state[] { repair_handle_state.售后处理中, repair_handle_state.技术售后处理中, repair_handle_state.审批完成, repair_handle_state.审批被驳回 }.Contains(o.repair_handle_state) ? true : false : false,
                                       now_state =
                                            (o.repair_handle_state == repair_handle_state.已关闭 || o.repair_handle_state == repair_handle_state.已完成) ? o.repair_handle_state : 0,
                                       o.repair_tel,
                                       o.repair_des_pic,
                                       o.express_no,
                                       o.approval_model,
                                       approval_model_str = o.approval_model.ToString(),
                                       o.express_model,
                                       o.approval_price,
                                       o.transfer_screenshot,
                                       o.express_time,
                                       o.express_state, 
                                       o.repair_content_pic
                                   };
                result.code = 0;
                result.msg = "success";
                result.data = result_model.FirstOrDefault();
                return result;
            });
        }
        /// <summary>
        ///  根据用户ID获取报修信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public ResultListModel get_repair_pagelist(Base_List_Params query)
        {
            return CoreStartDo.ResultAction<ResultListModel>(result =>
            {
                string sql = "select * from customer_repair";
                string where = " where 1=1 ";
                dynamic query_dy = new System.Dynamic.ExpandoObject();
                if (string.IsNullOrWhiteSpace(query.SerchKeyword))
                {
                    return new ResultListModel();
                }
                else
                {
                    where += " and user_id=@user_id";
                    query_dy.user_id = query.SerchKeyword;
                }
                ResultListModel<customer_repair> result_pagelist = _tool.di_db.GetPageList<customer_repair>(sql, where, "addtime", query_dy, query.IsPage, query.page, query.pagesize);
                var q = from o in result_pagelist.rows
                        select new
                        {
                            o.repair_id,//报修ID
                            o.addtime,//报修日期,
                            o.mch_sn,//机器SN,
                            o.repair_type,//报修类型,
                            o.repair_kid_type,//报修子类型,
                            o.customer_uname,//售后专员
                            o.technology_uname,//技术售后,
                            o.repair_handle_endtime,
                            o.repair_handle_state,//处理状态,
                            repair_handle_state_str = o.repair_handle_state.ToString(),
                            o.repair_tel,
                            o.repair_des_pic
                        };
                result.code = 0;
                result.msg = "success";
                result.rows = q.ToList();
                result.total = result_pagelist.total;
                result.current = query.page.Value;
                return result;
            });
        }
        /// <summary>
        ///  根据搜索条件获取登录人的报修信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public ResultListModel get_my_repair_pagelist(search_my_repair_list query)
        {
            return CoreStartDo.ResultAction<ResultListModel>(result =>
            {
                string sql = "select b.* from customer_repair_canshow a left join customer_repair b on a.repair_id = b.repair_id";
                string where = " where a.uid=@uid";
                dynamic query_dy = new System.Dynamic.ExpandoObject();
                query_dy.uid = _tool.di_user.USER.uid;
                where += " and addtime>=@addtime_sdt and addtime<=@addtime_edt";
                //提交时间查询
                if (!string.IsNullOrWhiteSpace(query.repair_sdt))
                {
                    query_dy.addtime_sdt = query.repair_sdt;
                }
                else
                {
                    query_dy.addtime_sdt = DateTime.Now.AddYears(-1);
                }
                //提交时间查询
                if (!string.IsNullOrWhiteSpace(query.repair_edt))
                {
                    query_dy.addtime_edt = query.repair_edt;
                }
                else
                {
                    query_dy.addtime_edt = DateTime.Now;
                }

                if (query.search_type.HasValue)
                {
                    switch (query.search_type.Value)
                    {
                        case search_enum_type.客户姓名:
                            where += " and user_name like @user_name";
                            query_dy.user_name = $"%{query.search_value}%";
                            break;
                        case search_enum_type.客户微信号:
                            where += " and user_wx_name like @user_wx_name";
                            query_dy.user_wx_name = $"%{query.search_value}%";
                            break;
                        case search_enum_type.客户电话:
                            where += " and user_tel like @user_tel";
                            query_dy.user_tel = $"%{query.search_value}%";
                            break;
                    }
                }
                //售后ID查询
                if (!string.IsNullOrWhiteSpace(query.customer_uid))
                {
                    where += " and customer_uid=@customer_uid";
                    query_dy.customer_uid = query.customer_uid;
                }
                //机器类型查询
                if (!string.IsNullOrWhiteSpace(query.mch_type))
                {
                    where += " and mch_type=@mch_type";
                    query_dy.mch_type = query.mch_type;
                }
                //机器sn查询
                if (!string.IsNullOrWhiteSpace(query.mch_sn))
                {
                    where += " and mch_sn like @mch_sn";
                    query_dy.mch_sn = $"%{query.mch_sn}%";
                }
                //报修类型查询
                if (!string.IsNullOrWhiteSpace(query.repair_type))
                {
                    where += " and repair_type=@repair_type";
                    query_dy.repair_type = query.repair_type;
                }
                //报修类型查询
                if (!string.IsNullOrWhiteSpace(query.repair_kid_type))
                {
                    where += " and repair_kid_type=@repair_kid_type";
                    query_dy.repair_kid_type = query.repair_kid_type;
                }
                if (query.repair_handle_state.HasValue)
                {
                    where += " and repair_handle_state=@repair_handle_state";
                    query_dy.repair_handle_state = query.repair_handle_state;
                } 
                if (query.express_state.HasValue)
                {
                    if (query.express_state.Value == express_state.无寄件)
                    {
                        where += " and (express_state is null or express_state=@express_state)"; 
                    }
                    else {
                        where += " and express_state=@express_state"; 
                    }
                    query_dy.express_state = query.express_state.Value;
                }
                if (query.return_approval_state.HasValue)
                {
                    if (query.return_approval_state.Value == return_approval_state.无需返件)
                    {
                        where += " and (reback_state is null or reback_state=@return_approval_state)";
                    }
                    else
                    {
                        where += " and reback_state=@return_approval_state";
                    }
                    query_dy.return_approval_state = query.return_approval_state.Value;
                }
                if (query.approval_model.HasValue)
                {
                    where += " and approval_model=@approval_model";
                    query_dy.approval_model = query.approval_model.Value;
                }
                ResultListModel<customer_repair> result_pagelist = _tool.di_db.GetPageList<customer_repair>(sql, where, "addtime", query_dy, query.IsPage, query.page, query.pagesize);
                var q = from o in result_pagelist.rows
                        select new
                        {
                            o.repair_id,//报修ID
                            o.addtime,//报修日期,
                            o.user_loginname,//客户账号
                            o.user_name,//客户名称,
                            o.user_wx_name,//微信号,
                            o.user_tel,///用户电话号, 
                            o.mch_type,//机器类型
                            o.mch_sn,//机器SN,
                            o.mch_assurance_sdt,//质保开始时间
                            o.repair_type,//报修类型,
                            o.repair_kid_type,//报修子类型,
                            o.customer_uname,//售后专员
                            o.technology_uname,//技术售后,
                            o.repair_handle_endtime,///处理时间
                            o.repair_handle_state,//处理状态,
                            repair_handle_state_str = o.repair_handle_state.ToString(),
                            o.repair_des_pic,
                            o.express_no,
                            o.express_state,
                            express_state_str = o.express_state.HasValue ? o.express_state.ToString() : express_state.无寄件.ToString(),
                            o.reback_state,
                            reback_state_str = o.reback_state.ToString(),
                            o.reback_result,
                            o.transfer_screenshot,
                            o.approval_model,
                            approval_model_str = o.approval_model.ToString()
                        };
                result.code = 0;
                result.msg = "success";
                result.rows = q.ToList();
                result.total = result_pagelist.total;
                result.current = query.page.Value;
                return result;
            });
        }

        /// <summary>
        ///  转技术售后
        /// </summary>
        /// <returns></returns>
        public ResultModel give_technology_repair(string repair_id, string technology_uid)
        {
            return CoreStartDo.ResultAction<ResultModel>(result =>
            {
                if (string.IsNullOrWhiteSpace(repair_id))
                {
                    result.code = 1;
                    result.msg = "参数异常,id未入参";
                    return result;
                }
                if (string.IsNullOrWhiteSpace(technology_uid))
                {
                    result.code = 1;
                    result.msg = "参数异常,uid未入参";
                    return result;
                }
                if (_tool.di_user.USER.role_id != "Customer" && _tool.di_user.USER.role_id != "all_Customer")
                {
                    result.code = 1;
                    result.msg = "您的权限无法执行该操作";
                    return result;
                }
                var checkrepair = _tool.di_db.GetModel<customer_repair>(new string[] { "repair_id=@repair_id" }, new { repair_id });
                if (checkrepair == null)
                {
                    result.code = 1;
                    result.msg = "报修信息不存在";
                    return result;
                }
                if (!string.IsNullOrWhiteSpace(checkrepair.technology_uid))
                {
                    result.code = 1;
                    result.msg = "已分配技术售后";
                    return result;
                }
                //验证uid是否存在
                var checkuser = _tool.di_db.GetModel<rk_user>(new string[] { "uid=@uid" }, new { uid = technology_uid });
                if (checkuser == null)
                {
                    result.code = 1;
                    result.msg = "所选技售后不存在";
                    return result;
                }
                else
                {
                    if (checkuser.role_id != "technology_Customer" && checkuser.role_id != "all_Customer")
                    {
                        result.code = 1;
                        result.msg = "选择售后角色不正确";
                        return result;
                    }
                }
                bool shouldaddcanshow = true;
                //如果提交报修的售后就是自己 并且是转到自己 那不需要新增可见权限
                if (checkrepair.customer_uid == technology_uid)
                {
                    shouldaddcanshow = false;
                }
                //开始转技术售后
                string error_msg = "";
                int tran_rows = _tool.di_db.StartTran((conn, tran) =>
                {
                    int rows = 0;
                    rows += conn.Execute("update customer_repair set technology_uid=@technology_uid,technology_uname=@technology_uname,repair_handle_state=@repair_handle_state where repair_id=@repair_id", new
                    {
                        technology_uid = checkuser.uid,
                        technology_uname = checkuser.u_name,
                        repair_handle_state = repair_handle_state.技术售后处理中,
                        repair_id = repair_id
                    }, tran);
                    if (shouldaddcanshow)
                    {
                        rows += conn.Execute(_tool.di_db.AddBySqlStr<customer_repair_canshow>(), new customer_repair_canshow() { uid = checkuser.uid, canshow_id = _tool.di_getid.IGet_SnowFlake_Id, repair_id = repair_id }, tran);
                    }
                    return rows;
                }, ref error_msg);
                if (tran_rows > 0)
                {
                    result.code = 0;
                    result.msg = "成功转到技术售后处理";
                }
                else
                {
                    result.code = 1;
                    result.msg = "转技术售后异常 " + error_msg;
                }
                return result;
            });
        }

      
        /// <summary>
        /// 申请审批
        /// </summary>
        /// <returns></returns>
        public ResultModel application_approval(string repair_id, string approval_uid, string repair_approval_des,
            approval_model? approval_model, string express_model, decimal? freight, decimal? approval_price, string express_no,string[] transfer_screenshot = null
            )
        {
            return CoreStartDo.ResultAction<ResultModel>(result =>
            {
                if (string.IsNullOrWhiteSpace(repair_id))
                {
                    result.code = 1;
                    result.msg = "参数异常,id未入参";
                    return result;
                }
                if (string.IsNullOrWhiteSpace(approval_uid))
                {
                    result.code = 1;
                    result.msg = "参数异常,approval_uid未入参";
                    return result;
                }
                if (_tool.di_user.USER.role_id != "technology_Customer" && _tool.di_user.USER.role_id != "all_Customer")
                {
                    result.code = 1;
                    result.msg = "只有技术售后可以提交审批";
                    return result;
                }
                var checkrepair = _tool.di_db.GetModel<customer_repair>(new string[] { "repair_id=@repair_id" }, new { repair_id });
                if (checkrepair == null)
                {
                    result.code = 1;
                    result.msg = "报修信息不存在";
                    return result;
                }
                if (checkrepair.repair_handle_state == repair_handle_state.技术售后处理中 && checkrepair.repair_handle_state == repair_handle_state.审批被驳回
                && checkrepair.repair_handle_state == repair_handle_state.审批处理中
                )
                {
                    result.code = 1;
                    result.msg = $"当前报修{checkrepair.repair_handle_state.ToString()},无法继续提交审批";
                    return result;
                }
                //if (!string.IsNullOrWhiteSpace(checkrepair.approval_uid))
                //{
                //    result.code = 1;
                //    result.msg = "已申请审批";
                //    return result;
                //}
                if (string.IsNullOrWhiteSpace(checkrepair.technology_uid))
                {
                    result.code = 1;
                    result.msg = "报修未转技术售后,不可提交审批";
                    return result;
                }
                //验证uid是否存在
                var checkuser = _tool.di_db.GetModel<rk_user>(new string[] { "uid=@uid" }, new { uid = approval_uid });
                if (checkuser == null)
                {
                    result.code = 1;
                    result.msg = "所选审批人不存在";
                    return result;
                }
                else
                {
                    if (checkuser.role_id != "Approval" && checkuser.role_id != "Finance_Manager")
                    {
                        result.code = 1;
                        result.msg = "所选审批人角色不正确";
                        return result;
                    }
                }
                if (transfer_screenshot == null)
                {
                    transfer_screenshot = new string[] { };
                }
                //开始转技术售后
                string error_msg = "";
                int tran_rows = _tool.di_db.StartTran((conn, tran) =>
                {
                    int rows = 0;
                    rows += conn.Execute("update customer_repair set approval_uid=@approval_uid,approval_name=@approval_name,repair_approval_des=@repair_approval_des,repair_handle_state=@repair_handle_state,repair_approval_addtime=@repair_approval_addtime,approval_model=@approval_model,express_model=@express_model,freight=@freight,approval_price=@approval_price,express_no=@express_no,transfer_screenshot=@transfer_screenshot where repair_id=@repair_id", new
                    {
                        approval_uid = checkuser.uid,
                        approval_name = checkuser.u_name,
                        repair_approval_des,
                        repair_handle_state = repair_handle_state.审批处理中,
                        repair_approval_addtime = DateTime.Now,
                        approval_model,
                        express_model,
                        freight,
                        approval_price,
                        repair_id,
                        express_no,
                        transfer_screenshot = Newtonsoft.Json.JsonConvert.SerializeObject(transfer_screenshot)
                    }, tran);
                    if (checkrepair.repair_handle_state == repair_handle_state.技术售后处理中)
                    {
                        rows += conn.Execute(_tool.di_db.AddBySqlStr<customer_repair_canshow>(), new customer_repair_canshow() { uid = checkuser.uid, canshow_id = _tool.di_getid.IGet_SnowFlake_Id, repair_id = repair_id }, tran);
                        //新增一条审批记录
                        rows += conn.Execute(_tool.di_db.AddBySqlStr<approval_record>(), add_new_record(approval_type.报修审核, "customer_repair", checkuser.uid, checkrepair.repair_id), tran);
                    }
                    return rows;
                }, ref error_msg);
                if (tran_rows > 0)
                {
                    result.code = 0;
                    result.msg = "申请审批成功";
                    approval_notify_void(notify_type.报修审批, repair_id,
                        $"http://www.shouhuojiyun.com/Rknew/MchApi/send_approval_alert?openid={checkuser.bindOpenid}&time={DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}&remark={_tool.di_user.USER.u_name}为客户{checkrepair.user_name}的机器{checkrepair.mch_name}提交了一个报修审核,请尽快前往处理.",
                         checkuser
                        );
                }
                else
                {
                    result.code = 1;
                    result.msg = "申请审批异常 " + error_msg;
                }
                return result;
            });
        }
        private approval_record add_new_record(approval_type type, string innertable, string apply_uid, string key_id) {
            approval_record result = new approval_record()
            {
                addtime = DateTime.Now,
                approval_type = type,
                approval_result = "",
                innertable = innertable,
                launch_name = _tool.di_user.USER.u_name,
                launch_uid = _tool.di_user.USER.uid,
                uid = apply_uid,
                record_id = _tool.di_getid.IGet_SnowFlake_Id,
                result_code = apply_state.审批处理中,
                key_id = key_id
            };
            return result;
        }
        /// <summary>
        /// 上传审批照片
        /// </summary>
        /// <returns></returns>
        public ResultModel upload_approval_pic(string repair_id, string repair_content_pic, string repair_express_no)
        {
            return CoreStartDo.ResultAction<ResultModel>(result =>
            {
                if (string.IsNullOrWhiteSpace(repair_id))
                {
                    result.code = 1;
                    result.msg = "参数异常,id未入参";
                    return result;
                }
                var checkrepair = _tool.di_db.GetModel<customer_repair>(new string[] { "repair_id=@repair_id" }, new { repair_id });
                if (checkrepair == null)
                {
                    result.code = 1;
                    result.msg = "报修信息不存在";
                    return result;
                }
                //开始转技术售后
                string error_msg = "";
                int tran_rows = _tool.di_db.StartTran((conn, tran) =>
                {
                    int rows = 0;
                    rows += conn.Execute("update customer_repair set repair_content_pic=@repair_content_pic,repair_express_no=@repair_express_no where repair_id=@repair_id", new
                    {
                        repair_content_pic,
                        repair_express_no,
                        repair_id
                    }, tran);
                    return rows;
                }, ref error_msg);
                if (tran_rows > 0)
                {
                    result.code = 0;
                    result.msg = "上传成功";
                }
                else
                {
                    result.code = 1;
                    result.msg = "上传失败 " + error_msg;
                }
                return result;
            });
        }
        /// <summary>
        /// 获取指定角色售后名单
        /// </summary>
        /// <returns></returns>
        public ResultObjModel get_customer_userlist(string lv, approval_model? approval_model)
        {
            return CoreStartDo.ResultAction(result =>
            {
                string sql = "select * from rk_user where 1=1";
                List<string> rolelist = new List<string>();
                switch (lv)
                {
                    case "all":
                        break;
                    case "customer":
                        sql += " and  role_id in @rolelist";
                        rolelist.Add("Customer");
                        rolelist.Add("all_Customer");
                        break;
                    case "technology":
                        sql += " and  role_id in @rolelist";
                        rolelist.Add("technology_Customer");
                        rolelist.Add("all_Customer");
                        break;
                    case "approval":
                        sql += " and  role_id in @rolelist";
                        if (!approval_model.HasValue || approval_model.Value == Entity.approval_model.在保审批)
                        {
                            rolelist.Add("Approval");
                        }
                        else
                        {
                            rolelist.Add("Finance_Manager");
                        }
                        break;
                    default:
                        sql += " and  1=0";
                        break;
                }
                var listresult = _tool.di_db.GetList<rk_user>(sql, new { rolelist = rolelist.ToArray() }).ToList().Select(s => new { s.uid, s.u_name, s.wx_name });
                result.code = 0;
                result.msg = "success";
                result.data = listresult;
                return result;
            });
        }
        /// <summary>
        /// 保存报修信息
        /// </summary>
        /// <returns></returns>
        public ResultModel save_repair(save_repair_params model) { 
            return CoreStartDo.ResultAction<ResultModel>(result =>
            {
                if (string.IsNullOrWhiteSpace(model.repair_id))
                {
                    result.code = 1;
                    result.msg = "报修ID未入参";
                    return result;
                }
                if (string.IsNullOrWhiteSpace(model.repair_type))
                {
                    result.code = 1;
                    result.msg = "请选择报修类型";
                    return result;
                }
                if (string.IsNullOrWhiteSpace(model.repair_kid_type))
                {
                    result.code = 1;
                    result.msg = "请选择报修子类型";
                    return result;
                }
                if (string.IsNullOrWhiteSpace(model.repair_des))
                {
                    result.code = 1;
                    result.msg = "报修问题描述必填";
                    return result;
                }
                if (!model.change_state.HasValue)
                {
                    result.code = 1;
                    result.msg = "保存时必须告知处理更改的状态";
                    return result;
                }
                var checkrepair = _tool.di_db.GetModel<customer_repair>(new string[] { "repair_id=@repair_id" }, new { model.repair_id });
                if (checkrepair == null)
                {
                    result.code = 1;
                    result.msg = "报修信息不存在";
                    return result;
                }
                //售后专员在技术售后接手后 无法进行保存
                if (_tool.di_user.USER.role_id == "Customer" && checkrepair.repair_handle_state >= repair_handle_state.技术售后处理中)
                {
                    result.code = 1;
                    result.msg = "当前报修已转到后续处理，您无法再进行保存";
                    return result;
                }
                //开始修改项 ,首先确认当前处理情况
                string state_change = "";
                if (model.change_state.Value == 6 || model.change_state.Value == 10) //完成 || //关闭
                {
                    if (checkrepair.repair_handle_state == repair_handle_state.已关闭 || checkrepair.repair_handle_state == repair_handle_state.已完成)
                    {
                        result.code = 1;
                        result.msg = "报修信息" + checkrepair.repair_handle_state.ToString() + ",不允许重复关闭";
                        return result;
                    }
                    //if ((int)checkrepair.repair_handle_state < 3 && model.change_state.Value == 6)
                    //{
                    //    result.code = 1;
                    //    result.msg = $"当前报修{checkrepair.repair_handle_state.ToString()},不允许完成";
                    //    return result;
                    //}
                    state_change = $",repair_handle_state='{model.change_state.Value }',repair_handle_endtime='{ DateTime.Now}'";
                }
                if (model.repair_des_pic == null)
                {
                    model.repair_des_pic = new string[] { };
                }
                model.repair_des_pic_str = Newtonsoft.Json.JsonConvert.SerializeObject(model.repair_des_pic); 
                //准备执行更新语句
                string sql = $"update customer_repair set repair_des_pic=@repair_des_pic_str,repair_type=@repair_type,repair_kid_type=@repair_kid_type,repair_des=@repair_des,technology_repair_handle_result=@technology_repair_handle_result,customer_repair_handle_result=@customer_repair_handle_result{state_change} where repair_id=@repair_id";
                int update_row = _tool.di_db.do_Execute(sql, model);
                if (update_row > 0)
                {
                    result.code = 0;
                    result.msg = "保存成功";
                }
                else
                {
                    result.code = 1;
                    result.msg = "保存失败";
                }
                return result;
            });
        }
        /// <summary>
        ///  根据搜索条件获取登录人的报修信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public ResultListModel<get_my_approval_pagelist_excel> get_my_approval_pagelist(search_my_approval_list query)
        {
            return CoreStartDo.ResultAction<ResultListModel<get_my_approval_pagelist_excel>>(result =>
            {
                string sql = "select b.* from customer_repair_canshow a left join customer_repair b on a.repair_id = b.repair_id";
                string where = " where a.uid=@uid";
                where += " and repair_approval_addtime>=@addtime_sdt and repair_approval_addtime<=@addtime_edt";
                dynamic query_dy = new System.Dynamic.ExpandoObject();
                //提交时间查询
                if (!string.IsNullOrWhiteSpace(query.addtime_sdt))
                {
                    query_dy.addtime_sdt = query.addtime_sdt;
                }
                else
                {
                    query_dy.addtime_sdt = DateTime.Now.AddYears(-1);
                }
                //提交时间查询
                if (!string.IsNullOrWhiteSpace(query.addtime_edt))
                {
                    query_dy.addtime_edt = query.addtime_edt;
                }
                else
                {
                    query_dy.addtime_edt = DateTime.Now;
                }
                query_dy.uid = _tool.di_user.USER.uid;
                if (!string.IsNullOrWhiteSpace(query.user_name))
                {
                    where += " and user_name like @user_name";
                    query_dy.user_name = $"%{query.user_name}%";

                }
                //技术售后查询
                if (!string.IsNullOrWhiteSpace(query.technology_uid))
                {
                    where += " and technology_uid=@technology_uid";
                    query_dy.technology_uid = query.technology_uid;
                }
                //审批状态查询
                if (query.repair_handle_state.HasValue)
                {
                    where += " and repair_handle_state=@repair_handle_state";
                    query_dy.repair_handle_state = query.repair_handle_state.Value;
                }
                //审核时间查询
                if (!string.IsNullOrWhiteSpace(query.repair_approval_time_sdt))
                {
                    where += " and repair_approval_time>=@repair_approval_time_sdt";
                    query_dy.repair_approval_time_sdt = query.repair_approval_time_sdt;
                }
                //审核时间查询
                if (!string.IsNullOrWhiteSpace(query.repair_approval_time_edt))
                {
                    where += " and repair_approval_time<=@repair_approval_time_edt";
                    query_dy.repair_approval_time_edt = query.repair_approval_time_edt;
                }
                if (query.have_pic.HasValue)
                {
                    if (query.have_pic.Value)
                    {
                        //查询有图片的
                        where += " and repair_express_no is not null and repair_express_no<>''";
                    }
                    else
                    {
                        //查询没图片的
                        where += " and (repair_express_no is null or repair_express_no='')";
                    }
                }
                if (query.reback_state.HasValue)
                {
                    where += " and reback_state=@reback_state";
                    query_dy.reback_state = query.reback_state;
                }
                if (!string.IsNullOrWhiteSpace(query.express_no))
                {
                    where += " and express_no=@express_no";
                    query_dy.express_no = query.express_no;
                }
                if (query.express_state.HasValue)
                {
                    if (query.express_state.Value == express_state.无寄件)
                    {
                        where += " and (express_state is null or express_state=@express_state)";
                    }
                    else
                    {
                        where += " and express_state=@express_state";
                    }
                    query_dy.express_state = query.express_state.Value;
                }
                ResultListModel<customer_repair> result_pagelist = _tool.di_db.GetPageList<customer_repair>(sql, where, "addtime", query_dy, query.IsPage, query.page, query.pagesize);
                var q = from o in result_pagelist.rows
                        select new get_my_approval_pagelist_excel
                        {
                            repair_id = o.repair_id,//报修ID
                            addtime = o.addtime,//报修日期, 
                            user_name = o.user_name,//客户名称, 
                            mch_assurance_sdt = o.mch_assurance_sdt,//质保开始时间
                            technology_uname = o.technology_uname,//提交人, 
                            repair_approval_addtime = o.repair_approval_addtime,//提交日期 
                            approval_name = o.approval_name,//审批人, 
                            repair_handle_state_str = ((repair_handle_state)o.repair_handle_state).ToString(),
                            repair_approval_time = o.repair_approval_time,//审批时间
                            repair_express_no = o.repair_express_no,
                            repair_content_pic = o.repair_content_pic,
                            hava_pic = string.IsNullOrWhiteSpace(o.repair_express_no) ? false : true,
                            repair_approval_des = o.repair_approval_des,
                            repair_des_pic = o.repair_des_pic,
                            repair_handle_state = o.repair_handle_state,
                            approval_price = o.approval_price, //审批金额
                            freight = o.freight,//运费
                            express_model = o.express_model,//快递模式, 
                            approval_model = o.approval_model,//审核模式
                            reback_state = o.reback_state,
                            reback_time = o.reback_time,
                            reback_result = o.reback_result,
                            reback_confirm_uid = o.reback_confirm_uid,
                            express_no = o.express_no,
                            express_state = o.express_state,
                            express_time = o.express_time,
                            express_state_str = o.express_state.HasValue ? o.express_state.ToString() : express_state.无寄件.ToString(),
                            reback_state_str = o.reback_state.ToString(),
                            transfer_screenshot = o.transfer_screenshot
                        };
                result.code = 0;
                result.msg = "success";
                result.rows = q.ToList();
                result.total = result_pagelist.total;
                result.current = query.page.Value;
                return result;
            });
        }

        public void down_my_approval_pagelist_for_excel(search_my_approval_list query)
        {
            query.IsPage = false;
            var listresult = get_my_approval_pagelist(query);
            string title = $"{DateTime.Now.ToString("yyyy-MM-dd")}_{_tool.di_user.USER.u_name}_下载超保审批统计.xlsx";
            _excelservice.ExcelExportService(title, listresult.rows, dosomething: fs =>
            {
                _context.HttpContext.Response.ContentType = "application/octet-stream;charset=UTF-8";
                _context.HttpContext.Response.Headers.Add("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(title, System.Text.Encoding.UTF8));
                int bufferSize = Convert.ToInt32(fs.Length);
                byte[] buffer;
                buffer = new byte[bufferSize];
                int currentRead = fs.Read(buffer, 0, bufferSize);
                _context.HttpContext.Response.Body.Write(buffer, 0, currentRead);
                _context.HttpContext.Response.Body.Flush();
            });
        }


 



        /// <summary>
        /// 处理审批
        /// </summary>
        /// <returns></returns>
        public ResultModel handle_apply(string[] repair_ids, apply_state? state, string approval_repair_handle_result)
        {
            return CoreStartDo.ResultAction<ResultModel>(result =>
            {
                if (_tool.di_user.USER.role_id != "Approval" && _tool.di_user.USER.role_id != "Finance_Manager")
                {
                    result.code = 1;
                    result.msg = "只有审核人才可以进行该操作";
                    return result;
                }
                if (repair_ids == null || !repair_ids.Any())
                {
                    result.code = 1;
                    result.msg = "请选择审核的报修项";
                    return result;
                }
                //if (string.IsNullOrWhiteSpace(repair_id))
                //{
                //    result.code = 1;
                //    result.msg = "参数异常,id未入参";
                //    return result;
                //}
                var checkrepair = _tool.di_db.GetModel<customer_repair>(new string[] { "repair_id in @repair_ids" }, new { repair_ids });
                if (checkrepair == null)
                {
                    result.code = 1;
                    result.msg = "报修信息不存在";
                    return result;
                }
                if (checkrepair.repair_handle_state == repair_handle_state.审批完成)
                {
                    result.code = 1;
                    result.msg = "当前审批处理已完成";
                    return result;
                }
                if (checkrepair.repair_handle_state != repair_handle_state.审批处理中)
                {
                    result.code = 1;
                    result.msg = "当前审批并不在处理中";
                    return result;
                }
                if (!state.HasValue)
                {
                    result.code = 1;
                    result.msg = "提交审核状态未入参";
                    return result;
                }
                //if (string.IsNullOrWhiteSpace(approval_repair_handle_result))
                //{
                //    result.code = 1;
                //    result.msg = "审核结论不能为空";
                //    return result;
                //}
                string error_msg = "";
                int tran_rows = _tool.di_db.StartTran((conn, tran) =>
                {
                    int rows = 0;
                    rows += conn.Execute("update customer_repair set " +
                        "repair_handle_state=@repair_handle_state,repair_approval_time=@repair_approval_time,approval_repair_handle_result=@approval_repair_handle_result " +
                        "where repair_id in @repair_ids",
                    new
                    {
                        repair_handle_state = (int)state.Value,
                        repair_approval_time = DateTime.Now,
                        approval_repair_handle_result,
                        repair_ids
                    }, tran);
                    rows += conn.Execute("update approval_record set approval_result+=@approval_result,result_code=@result_code where key_id in @key_ids", new
                    {
                        key_ids = repair_ids,
                        approval_result = "   ," + approval_repair_handle_result,
                        result_code = (int)state.Value
                    }, tran);
                    return rows;
                }, ref error_msg);
                if (tran_rows > 0)
                {
                    result.code = 0;
                    result.msg = "审核成功";

                    foreach (var item in repair_ids)
                    {
                        var checkuser = _tool.di_db.GetModel<rk_user>(new string[] { "uid=@uid" }, new { uid = checkrepair.technology_uid });
                        approval_notify_void(notify_type.报修审核结果通知, item,
                          $"http://www.shouhuojiyun.com/Rknew/MchApi/approval_result_alert?openid={checkuser.bindOpenid}&name={checkuser.u_name}&title={checkrepair.user_name}机器{checkrepair.mch_name}的报修{state.ToString()}&time={DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}&state={state.ToString()}&remark={checkrepair.user_name}机器{checkrepair.mch_name}的报修{state.ToString()},请及时关注审批通知,以便高效的进行下一步处理.",
                           checkuser
                          );
                    }
                }
                else
                {
                    result.code = 1;
                    result.msg = "审核失败 " + error_msg;
                }
                return result;
            });
        }

        /// <summary>
        /// 处理寄件状态
        /// </summary>
        /// <returns></returns>
        public ResultModel handle_apply_express_state(string repair_id, express_state? state)
        {
            return CoreStartDo.ResultAction<ResultModel>(result =>
            {
                if (_tool.di_user.USER.role_id != "Approval" && _tool.di_user.USER.role_id != "Finance_Manager")
                {
                    result.code = 1;
                    result.msg = "只有审核人才可以进行该操作";
                    return result;
                }
                if (string.IsNullOrWhiteSpace(repair_id))
                {
                    result.code = 1;
                    result.msg = "参数异常,id未入参";
                    return result;
                }
                var checkrepair = _tool.di_db.GetModel<customer_repair>(new string[] { "repair_id=@repair_id" }, new { repair_id });
                if (checkrepair == null)
                {
                    result.code = 1;
                    result.msg = "报修信息不存在";
                    return result;
                }
                //if (checkrepair.repair_handle_state == repair_handle_state.审批完成)
                //{
                //    result.code = 1;
                //    result.msg = "当前审批处理已完成";
                //    return result;
                //}
                //if (checkrepair.repair_handle_state != repair_handle_state.审批处理中)
                //{
                //    result.code = 1;
                //    result.msg = "当前审批并不在处理中";
                //    return result;
                //}
                if (!state.HasValue)
                {
                    result.code = 1;
                    result.msg = "提交寄件状态不能为空";
                    return result;
                }
                DateTime? express_time = null;
                if (state.Value == express_state.已寄出 && !checkrepair.express_time.HasValue)
                {
                    express_time = DateTime.Now;
                }
                string error_msg = "";
                int tran_rows = _tool.di_db.StartTran((conn, tran) =>
                {
                    int rows = 0;
                    rows += conn.Execute("update customer_repair set express_state=@express_state,express_time=@express_time where repair_id=@repair_id",
                    new
                    {
                        express_state = (int)state.Value,
                        express_time,
                        repair_id
                    }, tran);
                    return rows;
                }, ref error_msg);
                if (tran_rows > 0)
                {
                    result.code = 0;
                    result.msg = "修改寄件状态成功";
                }
                else
                {
                    result.code = 1;
                    result.msg = "修改寄件状态失败 " + error_msg;
                }
                return result;
            });
        }

        /// <summary>
        ///  查看所有报修信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public ResultListModel admin_get_repair_pagelist(search_my_repair_list query)
        {
            return CoreStartDo.ResultAction<ResultListModel>(result =>
            {
                string sql = "select * from customer_repair";
                string where = " where 1=1";
                dynamic query_dy = new System.Dynamic.ExpandoObject();
                where += " and addtime>=@addtime_sdt and addtime<=@addtime_edt";
                //提交时间查询
                if (!string.IsNullOrWhiteSpace(query.repair_sdt))
                {
                    query_dy.addtime_sdt = query.repair_sdt;
                }
                else
                {
                    query_dy.addtime_sdt = DateTime.Now.AddYears(-1);
                }
                //提交时间查询
                if (!string.IsNullOrWhiteSpace(query.repair_edt))
                {
                    query_dy.addtime_edt = query.repair_edt;
                }
                else
                {
                    query_dy.addtime_edt = DateTime.Now;
                }

                if (query.search_type.HasValue)
                {
                    switch (query.search_type.Value)
                    {
                        case search_enum_type.客户姓名:
                            where += " and user_name like @user_name";
                            query_dy.user_name = $"%{query.search_value}%";
                            break;
                        case search_enum_type.客户微信号:
                            where += " and user_wx_name like @user_wx_name";
                            query_dy.user_wx_name = $"%{query.search_value}%";
                            break;
                        case search_enum_type.客户电话:
                            where += " and user_tel like @user_tel";
                            query_dy.user_tel = $"%{query.search_value}%";
                            break;
                    }
                }
                //售后ID查询
                if (!string.IsNullOrWhiteSpace(query.customer_uid))
                {
                    where += " and customer_uid=@customer_uid";
                    query_dy.customer_uid = query.customer_uid;
                }
                //机器类型查询
                if (!string.IsNullOrWhiteSpace(query.mch_type))
                {
                    where += " and mch_type=@mch_type";
                    query_dy.mch_type = query.mch_type;
                }
                //机器sn查询
                if (!string.IsNullOrWhiteSpace(query.mch_sn))
                {
                    where += " and mch_sn like @mch_sn";
                    query_dy.mch_sn = $"%{query.mch_sn}%";
                }
                //报修类型查询
                if (!string.IsNullOrWhiteSpace(query.repair_type))
                {
                    where += " and repair_type=@repair_type";
                    query_dy.repair_type = query.repair_type;
                }
                //报修类型查询
                if (!string.IsNullOrWhiteSpace(query.repair_kid_type))
                {
                    where += " and repair_kid_type=@repair_kid_type";
                    query_dy.repair_kid_type = query.repair_kid_type;
                }
                if (query.repair_handle_state.HasValue)
                {
                    where += " and repair_handle_state=@repair_handle_state";
                    query_dy.repair_handle_state = query.repair_handle_state;
                }
                ResultListModel<customer_repair> result_pagelist = _tool.di_db.GetPageList<customer_repair>(sql, where, "addtime", query_dy, query.IsPage, query.page, query.pagesize);
                var q = from o in result_pagelist.rows
                        select new
                        {
                            o.repair_id,//报修ID
                            o.addtime,//报修日期,
                            o.user_loginname,//客户账号
                            o.user_name,//客户名称,
                            o.user_wx_name,//微信号,
                            o.user_tel,///用户电话号, 
                            o.mch_type,//机器类型
                            o.mch_sn,//机器SN,
                            o.mch_assurance_sdt,//质保开始时间
                            o.repair_type,//报修类型,
                            o.repair_kid_type,//报修子类型,
                            o.customer_uname,//售后专员
                            o.technology_uname,//技术售后,
                            o.repair_handle_endtime,///处理时间
                            o.repair_handle_state,//处理状态,
                            repair_handle_state_str = o.repair_handle_state.ToString(),
                            o.repair_approval_addtime,//提交日期 
                            o.approval_name,//审批人,  
                            o.repair_approval_time,//审批时间
                            o.repair_express_no,
                            o.repair_content_pic,
                            hava_pic = string.IsNullOrWhiteSpace(o.repair_express_no) ? false : true,
                        };
                result.code = 0;
                result.msg = "success";
                result.rows = q.ToList();
                result.total = result_pagelist.total;
                result.current = query.page.Value;
                return result;
            });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ResultObjModel get_repair_express(string repair_id)
        {
            return CoreStartDo.ResultAction(result =>
            {
                var repair_model = _tool.di_db.GetList<customer_repair>("select top 1 * from customer_repair where repair_id=@repair_id", new { repair_id }).Select(s => new
                {
                    s.repair_id,
                    s.repair_express_no,
                    s.repair_content_pic
                }).FirstOrDefault();
                result.code = 0;
                result.msg = "success";
                result.data = repair_model;
                return result;
            });
        }


        private string[] get_repair_pagelist_for_excel_return_column = new string[] {
            "a.*",
            "b.repair_return_id",
            "b.return_type",
            "b.return_express_no",
            "b.return_des",
            "b.return_approval_state",
            "b.return_approval_des",
            "b.return_sort",
            "b.return_pic",
            "b.submit_uname",
            "b.return_approval_name",
            "b.approval_time"
        };

        //customer_repairForexcelPage
        /// <summary>
        ///  查询报修信息 For excel
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public ResultListModel<customer_repairForexcelPage> get_repair_pagelist_for_excel(search_my_repair_list_for_excel query)
        {
            return CoreStartDo.ResultAction<ResultListModel<customer_repairForexcelPage>>(result =>
            {
                string sql = $"select {string.Join(',', get_repair_pagelist_for_excel_return_column)} from customer_repair a left join customer_repair_return b on a.repair_id=b.repair_id and b.is_last='true'";
                string where = " where 1=1";
                dynamic query_dy = new System.Dynamic.ExpandoObject();
                if (!string.IsNullOrWhiteSpace(query.user_name))
                {
                    where += " and (a.user_name like @user_name or a.user_loginname like @user_name)";
                    query_dy.user_name = $"%{query.user_name}%"; 
                }
                if (!string.IsNullOrWhiteSpace(query.customer_uid))
                {
                    where += " and a.customer_uid=@customer_uid";
                    query_dy.customer_uid = query.customer_uid;
                }

                if (!string.IsNullOrWhiteSpace(query.technology_uid))
                {
                    where += " and a.technology_uid=@technology_uid";
                    query_dy.technology_uid = query.technology_uid;
                }
                if (!string.IsNullOrWhiteSpace(query.mch_type))
                {
                    where += " and a.mch_type=@mch_type";
                    query_dy.mch_type = query.mch_type;
                }
                if (!string.IsNullOrWhiteSpace(query.mch_sn))
                {
                    where += " and a.mch_sn like @mch_sn";
                    query_dy.mch_sn = $"%{query.mch_sn}%";
                }
                //报修类型查询
                if (!string.IsNullOrWhiteSpace(query.repair_type))
                {
                    where += " and a.repair_type=@repair_type";
                    query_dy.repair_type = query.repair_type;
                }
                //报修类型查询
                if (!string.IsNullOrWhiteSpace(query.repair_kid_type))
                {
                    where += " and a.repair_kid_type=@repair_kid_type";
                    query_dy.repair_kid_type = query.repair_kid_type;
                }
                ///处理状态搜索
                if (query.repair_handle_state.HasValue)
                {
                    where += " and a.repair_handle_state=@repair_handle_state";
                    query_dy.repair_handle_state = query.repair_handle_state;
                }
                //处理时间搜索
                if (!string.IsNullOrWhiteSpace(query.repair_handle_endtime_sdt))
                {
                    where += " and a.repair_handle_endtime>=@repair_handle_endtime_sdt";
                    query_dy.repair_handle_endtime_sdt = query.repair_handle_endtime_sdt;
                }
                if (!string.IsNullOrWhiteSpace(query.repair_handle_endtime_edt))
                {
                    where += " and a.repair_handle_endtime<=@repair_handle_endtime_edt";
                    query_dy.repair_handle_endtime_edt = query.repair_handle_endtime_edt;
                }
                //报修时间搜索
                if (!string.IsNullOrWhiteSpace(query.repair_sdt))
                {
                    where += " and a.repair_time>=@repair_sdt";
                    query_dy.repair_sdt = query.repair_sdt;
                }
                if (!string.IsNullOrWhiteSpace(query.repair_edt))
                {
                    where += " and a.repair_time<=@repair_edt";
                    query_dy.repair_edt = query.repair_edt;
                }
                //质保开始时间
                if (!string.IsNullOrWhiteSpace(query.mch_assurance_sdt_sdt))
                {
                    where += " and a.mch_assurance_sdt>=@mch_assurance_sdt_sdt";
                    query_dy.mch_assurance_sdt_sdt = query.mch_assurance_sdt_sdt;
                }
                if (!string.IsNullOrWhiteSpace(query.mch_assurance_sdt_edt))
                {
                    where += " and a.mch_assurance_sdt<=@mch_assurance_sdt_edt";
                    query_dy.mch_assurance_sdt_edt = query.mch_assurance_sdt_edt;
                }
                //审批人
                if (!string.IsNullOrWhiteSpace(query.approval_uid))
                {
                    where += " and a.approval_uid=@approval_uid";
                    query_dy.approval_uid = query.approval_uid;
                }
                ///审批状态
                if (query.repair_handle_state.HasValue)
                {
                    where += " and a.repair_handle_state=@repair_handle_state";
                    query_dy.repair_handle_state = query.repair_handle_state;
                }
                //审批时间查询
                if (!string.IsNullOrWhiteSpace(query.repair_approval_time_sdt))
                {
                    where += " and a.repair_approval_addtime>=@repair_approval_time_sdt";
                    query_dy.repair_approval_time_sdt = query.repair_approval_time_sdt;
                }
                if (!string.IsNullOrWhiteSpace(query.repair_approval_time_edt))
                {
                    where += " and a.repair_approval_addtime<=@repair_approval_time_edt";
                    query_dy.repair_approval_time_edt = query.repair_approval_time_edt;
                }
                if (query.have_express_no.HasValue)
                {
                    if (query.have_express_no.HasValue)
                    {
                        where += " and a.repair_express_no is not null and a.repair_express_no<>''";
                    }
                    else
                    {
                        where += " and (a.repair_express_no is not null or a.repair_express_no='')";
                    }
                }
                if (query.return_type.HasValue)
                {
                    where += " and b.return_type=@return_type";
                    query_dy.return_type = query.return_type.Value;
                }
                if (query.return_approval_state.HasValue)
                {
                    if (query.return_approval_state.Value != return_approval_state.无需返件)
                    {
                        where += " and b.return_approval_state=@return_approval_state";
                        query_dy.return_approval_state = query.return_approval_state.Value;
                    }
                    else
                    {
                        where += " and b.repair_return_id is null";

                    }
                }
                //审批次数查询
                if (query.return_sort.HasValue)
                {
                    
                    if (query.return_sort.Value == 0)
                    {
                        where += " and b.return_sort is null";
                    }
                    else
                    {
                        where += " and b.return_sort=@return_sort";
                        query_dy.return_sort = query.return_sort.Value; 
                    }
                }

                ResultListModel<customer_repair_for_statistics> result_pagelist = _tool.di_db.GetPageList<customer_repair_for_statistics>(sql, where, "addtime", query_dy, query.IsPage, query.page, query.pagesize);
                var q = from o in result_pagelist.rows
                        select new customer_repairForexcelPage
                        {
                            //user_loginname = o.user_loginname,
                            repair_time = o.repair_time.HasValue ? o.repair_time.Value.ToString("yyyy-MM-dd") : "error",
                            user_name = o.user_name,
                            mch_type = o.mch_type,
                            mch_sn = o.mch_sn,
                            mch_name = o.mch_name,
                            mch_assurance_sdt = o.mch_assurance_sdt.HasValue ? o.mch_assurance_sdt.Value.ToString("yyyy-MM-dd") : "未设置",
                            repair_type = o.repair_type,
                            repair_kid_type = o.repair_kid_type,
                            repair_des = o.repair_des,
                            customer_uname = o.customer_uname,
                            customer_repair_handle_result = o.customer_repair_handle_result,
                            technology_uname = o.technology_uname,
                            technology_repair_handle_result = o.technology_repair_handle_result,
                            repair_handle_state = o.repair_handle_state,
                            repair_handle_endtime = o.repair_handle_endtime,
                            approval_name = o.approval_name,
                            repair_approval_time = o.repair_approval_time,
                            repair_express_no = o.repair_express_no,
                            reback_state = o.reback_state,
                            reback_time = o.reback_time,
                            reback_result = o.reback_result,
                            repair_approval_des = o.repair_approval_des,
                            approval_time = o.approval_time,
                            repair_return_id = o.repair_return_id,
                            return_sort = o.return_sort,
                            return_approval_des = o.return_approval_des,
                            return_approval_name = o.return_approval_name,
                            return_approval_state = o.return_approval_state,
                            return_des = o.return_des,
                            return_type = o.return_type,
                            submit_uname = o.submit_uname,
                            return_pic = o.return_pic,
                            express_no = o.express_no,
                            return_express_no = o.return_express_no,
                            repair_id = o.repair_id
                        };
                result.code = 0;
                result.msg = "success";
                result.rows = q.ToList();
                result.total = result_pagelist.total;
                result.current = query.page.Value;
                return result;
            });
        }


        public void down_repair_pagelist_for_excel(search_my_repair_list_for_excel query)
        {
            query.IsPage = false;
            var listresult = get_repair_pagelist_for_excel(query);
            string title = $"{DateTime.Now.ToString("yyyy-MM-dd")}_{_tool.di_user.USER.u_name}_下载报修统计.xlsx";
            _excelservice.ExcelExportService(title, listresult.rows, dosomething: fs =>
             {
                 _context.HttpContext.Response.ContentType = "application/octet-stream;charset=UTF-8";
                 _context.HttpContext.Response.Headers.Add("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(title, System.Text.Encoding.UTF8));
                 int bufferSize = Convert.ToInt32(fs.Length);
                 byte[] buffer;
                 buffer = new byte[bufferSize];
                 int currentRead = fs.Read(buffer, 0, bufferSize);
                 _context.HttpContext.Response.Body.Write(buffer, 0, currentRead);
                 _context.HttpContext.Response.Body.Flush();
             });
        }
        /// <summary>
        /// 记录一次通知，然后直接发送一次
        /// </summary>
        /// <param name="notify_type"></param>
        /// <param name="send_content"></param>
        private void approval_notify_void(notify_type? notify_type, string repair_id, string send_content, rk_user checkuser)
        {
            approval_notify send_notify = new approval_notify()
            {
                id = _tool.di_getid.IGet_SnowFlake_Id,
                addtime = DateTime.Now,
                alert_count = 1,
                is_end = false,
                last_notify_time = DateTime.Now,
                notify_type = notify_type.Value,
                bussiness_id = repair_id,
                uid = checkuser.uid,
                send_content = send_content
            };
            if (checkuser == null || string.IsNullOrWhiteSpace(checkuser.bindOpenid))
            {
                send_notify.is_end = true;
                send_notify.remark = "待发送人未绑定微信号";
            }
            if (notify_type.Value != Entity.notify_type.报修审批)
            {
                send_notify.is_end = true;
            }
            _tool.di_db.Add(send_notify);
            if (checkuser != null && !string.IsNullOrWhiteSpace(checkuser.bindOpenid))
            {
                Task.Factory.StartNew(url =>
                {
                    Http_Helper.HttpGet(url.ToString());
                }, send_content);
            }
        }

        /// <summary>
        /// 返回配件处理
        /// </summary>
        /// <returns></returns>
        public ResultModel reback_data_info(reback_data_info_params Params)
        {
            return CoreStartDo.ResultAction(result =>
            {
                if (string.IsNullOrWhiteSpace(Params.repair_id))
                {
                    result.code = 1;
                    result.msg = "报修ID未上传";
                    return result;
                }
                if (!Params.reback_state.HasValue)
                {
                    result.code = 1;
                    result.msg = "必须定义返回配件状态";
                    return result;
                }
                var repair_mod = _tool.di_db.GetList<customer_repair>("select * from customer_repair where repair_id=@repair_id", new { Params.repair_id }).FirstOrDefault();
                if (repair_mod == null)
                {
                    result.code = 1;
                    result.msg = "报修记录不存在";
                    return result;
                }
                string ext_flt = "";
                if (!repair_mod.reback_time.HasValue)
                {
                    ext_flt = ",reback_time=@reback_time";
                }
                int updaterow = _tool.di_db.do_Execute($"update customer_repair set reback_state=@reback_state,reback_result=@reback_result,reback_confirm_uid=@reback_confirm_uid{ext_flt} where repair_id=@repair_id", new
                {
                    reback_state = Params.reback_state,
                    reback_result = Params.reback_result,
                    reback_confirm_uid = _tool.di_user.USER.uid,
                    reback_time = DateTime.Now,
                    repair_id = Params.repair_id
                });
                if (updaterow > 0)
                {
                    result.code = 0;
                    result.msg = "更新成功";
                }
                else
                {
                    result.code = 1;
                    result.msg = "更新异常";
                }
                return result;
            });
        }


        /// <summary>
        ///  审批统计列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public ResultListModel<get_approval_pagelist_for_Excel> get_approval_pagelist(search_my_approval_list query)
        {
            return CoreStartDo.ResultAction<ResultListModel<get_approval_pagelist_for_Excel>>(result =>
            {
                //string sql = "select b.* from customer_repair b";
                string sql = "select * from (select b.*,(select count(*) from customer_repair_return c where b.repair_id=c.repair_id) as return_count from customer_repair b) t";
                string where = " where 1=1";
                where += " and repair_approval_addtime>=@addtime_sdt and repair_approval_addtime<=@addtime_edt";
                dynamic query_dy = new System.Dynamic.ExpandoObject();
                //提交时间查询
                if (!string.IsNullOrWhiteSpace(query.addtime_sdt))
                {
                    query_dy.addtime_sdt = query.addtime_sdt;
                }
                else
                {
                    query_dy.addtime_sdt = DateTime.Now.AddYears(-1);
                }
                //提交时间查询
                if (!string.IsNullOrWhiteSpace(query.addtime_edt))
                {
                    query_dy.addtime_edt = query.addtime_edt;
                }
                else
                {
                    query_dy.addtime_edt = DateTime.Now;
                }
                query_dy.uid = _tool.di_user.USER.uid;
                if (!string.IsNullOrWhiteSpace(query.user_name))
                {
                    where += " and user_name like @user_name";
                    query_dy.user_name = $"%{query.user_name}%";

                }
                //技术售后查询
                if (!string.IsNullOrWhiteSpace(query.technology_uid))
                {
                    where += " and technology_uid=@technology_uid";
                    query_dy.technology_uid = query.technology_uid;
                }
                //审批状态查询
                if (query.repair_handle_state.HasValue)
                {
                    where += " and repair_handle_state=@repair_handle_state";
                    query_dy.repair_handle_state = query.repair_handle_state.Value;
                }
                //审核时间查询
                if (!string.IsNullOrWhiteSpace(query.repair_approval_time_sdt))
                {
                    where += " and repair_approval_time>=@repair_approval_time_sdt";
                    query_dy.repair_approval_time_sdt = query.repair_approval_time_sdt;
                }
                //审核时间查询
                if (!string.IsNullOrWhiteSpace(query.repair_approval_time_edt))
                {
                    where += " and repair_approval_time<=@repair_approval_time_edt";
                    query_dy.repair_approval_time_edt = query.repair_approval_time_edt;
                }
                if (query.have_pic.HasValue)
                {
                    if (query.have_pic.Value)
                    {
                        //查询有图片的
                        where += " and repair_express_no is not null and repair_express_no<>''";
                    }
                    else
                    {
                        //查询没图片的
                        where += " and (repair_express_no is null or repair_express_no='')";
                    }
                }
                if (query.reback_state.HasValue)
                {
                    where += " and reback_state=@reback_state";
                    query_dy.reback_state = query.reback_state;
                }
                if (query.approval_model.HasValue)
                {
                    where += " and approval_model=@approval_model";
                    query_dy.approval_model = query.approval_model.Value;
                }
                ///寄件状态查询
                if (query.express_state.HasValue)
                {
                    if (query.express_state.Value == express_state.无寄件)
                    {
                        where += " and (express_state is null or express_state=@express_state)";
                    }
                    else
                    {
                        where += " and express_state=@express_state";
                    }
                    query_dy.express_state = query.express_state.Value;
                }
                //寄件时间查询
                if (!string.IsNullOrWhiteSpace(query.express_time_sdt))
                {
                    where += " and express_time>=@express_time_sdt";
                    query_dy.express_time_sdt = query.express_time_sdt;
                }
                //寄件时间查询
                if (!string.IsNullOrWhiteSpace(query.express_time_edt))
                {
                    where += " and express_time<=@express_time_edt";
                    query_dy.express_time_edt = query.express_time_edt;
                }
                DateTime alert_time = DateTime.Now.AddDays(-15); //定义统计临界点
                if (query.express_time_out_type.HasValue)
                { 
                    where += " and express_time>=@min_alert_time and express_time<@alert_time and return_count=0"; 
                    switch (query.express_time_out_type)
                    {
                        case express_time_out_type.小于等于1天: 
                            query_dy.min_alert_time = alert_time.AddDays(-1);  //统计开始日期为临界值-1天
                            query_dy.alert_time = alert_time; //统计截止日期为临界值本身
                            break;
                        case express_time_out_type.大于1天小于等于7天:
                            query_dy.min_alert_time = alert_time.AddDays(-7);  //统计开始日期为临界值-7天
                            query_dy.alert_time = alert_time.AddDays(-1); //统计截止为临界值-1天
                            break;
                        case express_time_out_type.大于7天小于等于15天:
                            query_dy.min_alert_time = alert_time.AddDays(-15); //统计开始日期为临界值-15天
                            query_dy.alert_time = alert_time.AddDays(-7); //统计截止为临界值-1天再-8天
                            break;
                        case express_time_out_type.大于15天:
                            query_dy.min_alert_time = alert_time.AddYears(-10); //设置一个阴间活  设置开始最小时间为10年前 保证能请求到数据
                            query_dy.alert_time = alert_time.AddDays(-15); //统计截止为临界值-1天再-8天
                            break; 
                    }
                } 
                ResultListModel<customer_repair_for_return_search> result_pagelist = _tool.di_db.GetPageList<customer_repair_for_return_search>(sql, where, "addtime", query_dy, query.IsPage, query.page, query.pagesize);
                var q = from o in result_pagelist.rows
                        select new get_approval_pagelist_for_Excel
                        {
                            repair_id = o.repair_id,//报修ID
                            addtime = o.addtime,//报修日期, 
                            user_name = o.user_name,//客户名称, 
                            mch_assurance_sdt = o.mch_assurance_sdt,//质保开始时间
                            technology_uname = o.technology_uname,//提交人, 
                            repair_approval_addtime = o.repair_approval_addtime,//提交日期 
                            approval_name = o.approval_name,//审批人, 
                            repair_handle_state_str = ((repair_handle_state)o.repair_handle_state).ToString(),
                            repair_approval_time = o.repair_approval_time,//审批时间
                            repair_express_no = o.repair_express_no,
                            repair_content_pic = o.repair_content_pic,
                            hava_pic = string.IsNullOrWhiteSpace(o.repair_express_no) ? false : true,
                            repair_approval_des = o.repair_approval_des,
                            repair_des_pic = o.repair_des_pic,
                            repair_handle_state = o.repair_handle_state,
                            approval_price = o.approval_price, //审批金额
                            freight = o.freight,//运费
                            express_model = o.express_model,//快递模式, 
                            approval_model = o.approval_model,//审核模式
                            reback_state = o.reback_state,
                            reback_time = o.reback_time,
                            reback_result = o.reback_result,
                            reback_confirm_uid = o.reback_confirm_uid,
                            approval_repair_handle_result = o.approval_repair_handle_result,
                            return_count = o.return_count,
                            express_no = o.express_no,
                            express_state = o.express_state,
                            express_state_str = o.express_state.HasValue ? o.express_state.ToString() : express_state.无寄件.ToString(),
                            express_time = o.express_time,
                            return_out_des = !o.express_time.HasValue ? "寄件未发" :
                                                o.express_time.Value < alert_time ? Convert.ToInt32((alert_time - o.express_time.Value).TotalDays) == 0 ? "已超期" :
                                                    $"超期{(alert_time - o.express_time.Value).TotalDays}日" : o.return_count == 0 ? "无返件提报" : "未超期"
                        };
                result.code = 0;
                result.msg = "success";
                result.rows = q.ToList();
                result.total = result_pagelist.total;
                result.current = query.page.Value;
                return result;

            });
        }
        /// <summary>
        /// 审批统计列表 下载
        /// </summary>
        /// <param name="query"></param>
        public void down_approval_pagelist_for_excel(search_my_approval_list query)
        {
            query.IsPage = false;
            var listresult = get_approval_pagelist(query);
            string title = $"{DateTime.Now.ToString("yyyy-MM-dd")}_{_tool.di_user.USER.u_name}_下载报修统计.xlsx";
            _excelservice.ExcelExportService(title, listresult.rows, dosomething: fs =>
            {
                _context.HttpContext.Response.ContentType = "application/octet-stream;charset=UTF-8";
                _context.HttpContext.Response.Headers.Add("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(title, System.Text.Encoding.UTF8));
                int bufferSize = Convert.ToInt32(fs.Length);
                byte[] buffer;
                buffer = new byte[bufferSize];
                int currentRead = fs.Read(buffer, 0, bufferSize);
                _context.HttpContext.Response.Body.Write(buffer, 0, currentRead);
                _context.HttpContext.Response.Body.Flush();
            });
        }



        #region 返件相关处理
        /// <summary>
        /// 提交返件申请
        /// </summary>
        /// <returns></returns>
        public ResultObjModel submit_return_approval(submit_return_approval_params model)
        {
            return CoreStartDo.ResultAction(result =>
            {
                if (string.IsNullOrWhiteSpace(model.repair_id))
                {
                    result.code = 1;
                    result.msg = "报修ID不能为空";
                    return result;
                }
                if (!model.return_type.HasValue)
                {
                    result.code = 1;
                    result.msg = "返件类型不能为空";
                    return result;
                }
                if (!model.temp_save.HasValue)
                {
                    result.code = 1;
                    result.msg = "必须告知储存模式";
                    return result;
                }
                var checkrepair = _tool.di_db.GetModel<customer_repair>(new string[] { "repair_id=@repair_id" }, new { model.repair_id });
                if (checkrepair == null)
                {
                    result.code = 1;
                    result.msg = "报修信息不存在";
                    return result;
                }
                //尝试获取当前一条的
                customer_repair_return check_customer_repair_return = _tool.di_db.GetModel<customer_repair_return>(new string[] { "repair_return_id=@repair_return_id" }, new { model.repair_return_id });
                //获取已提交数据列表
                var repair_list = _tool.di_db.GetList<customer_repair_return>("select * from customer_repair_return  where repair_id=@repair_id", new { model.repair_id });
                if (!model.temp_save.Value)
                {
                    //如果当前并非临时储存,则开始进行正式判断
                    if (model.return_type.Value == return_type.同意返件)
                    {
                        if (string.IsNullOrWhiteSpace(model.return_express_no))
                        {
                            result.code = 1;
                            result.msg = "返件单号不能为空";
                            return result;
                        }
                    }
                    else if (model.return_type.Value == return_type.不同意返件)
                    {
                        if (string.IsNullOrWhiteSpace(model.return_des))
                        {
                            result.code = 1;
                            result.msg = "说明不能为空";
                            return result;
                        }
                        if (model.return_pic == null || !model.return_pic.Any())
                        {
                            result.code = 1;
                            result.msg = "至少上传一张图片";
                            return result;
                        }
                    } 
                    if (repair_list.Where(s => s.return_approval_state == return_approval_state.返件审批已完成).Any())
                    {
                        result.code = 1;
                        result.msg = "当前报修审批已完成";
                        return result;
                    }
                    if (!string.IsNullOrWhiteSpace(model.repair_return_id))
                    {
                        //如果是提交的修改，则需要判断当前返件请求是否可以重复修改 
                        if (check_customer_repair_return == null)
                        {
                            result.code = 1;
                            result.msg = "修改提交的返件处理不存在";
                            return result;
                        }
                        if (!check_customer_repair_return.is_last)
                        {
                            result.code = 1;
                            result.msg = "非法操作:无法重复提交已关闭的审核";
                            return result;
                        }
                        if (!check_customer_repair_return.can_update)
                        {
                            result.code = 1;
                            result.msg = $"非法操作:当前审核状态{check_customer_repair_return.return_approval_state.ToString()}不允许修改,无法重复提交";
                            return result;
                        }
                    }
                    else
                    {
                        //当前添加审批,需要判断针对此次报修提交次数是否超过3次
                        var sub_count = repair_list.Count();//_tool.di_db.GetList<int>("select count(*) from customer_repair_return  where repair_id=@repair_id", new { model.repair_id }).FirstOrDefault();
                        if (sub_count == 3)
                        {
                            //当前已经有3次提交数据了,不允许再进行提交
                            result.code = 1;
                            result.msg = "当次报修已提交了三次返件审核，无法继续提交";
                            return result;
                        }
                        var can_update_rows = repair_list.Where(s => s.can_update == true).Count();//_tool.di_db.GetList<int>("select count(*) from customer_repair_return where repair_id=@repair_id and can_update=@can_update", new { model.repair_id, can_update = true }).FirstOrDefault();
                        if (can_update_rows > 0)
                        {
                            result.code = 1;
                            result.msg = "非法操作:当前存在可以修改的返件提交,无法提交新的申请";
                            return result;
                        }
                        var add_sub_count = repair_list.Where(s => new return_approval_state[] {
                            return_approval_state.保存未提交 , return_approval_state.返件审批已提交}.Contains(s.return_approval_state)).Count(); //_tool.di_db.GetList<int>("select count(*) from customer_repair_return  where repair_id=@repair_id and return_approval_state=@return_approval_state", new { model.repair_id, return_approval_state = return_approval_state.保存未提交 }).FirstOrDefault();
                        if (add_sub_count > 0)
                        {
                            result.code = 1;
                            result.msg = "已存在临时保存的提交, 请确认状态后再重新提交";
                            return result;
                        }
                    }
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(model.repair_return_id))
                    {
                        //如果是添加数据，则需要判断是否还有其他临时保存的数据
                        //如果当前是临时保存的数据 则需要判断，是否存在同样临时保存的数据
                        var sub_count = repair_list.Where(s => new return_approval_state[] {
                            return_approval_state.保存未提交 , return_approval_state.返件审批已提交}.Contains(s.return_approval_state)).Count();  //_tool.di_db.GetList<int>("select count(*) from customer_repair_return  where repair_id=@repair_id and return_approval_state=@return_approval_state", new { model.repair_id, return_approval_state = return_approval_state.保存未提交 }).FirstOrDefault();
                        if (sub_count > 0)
                        {
                            result.code = 1;
                            result.msg = "已存在临时保存的提交, 请确认状态后再重新提交";
                            return result;
                        }
                    }
                    else {
                        if (!check_customer_repair_return.can_update)
                        {
                            result.code = 1;
                            result.msg = $"非法操作:当前审核状态{check_customer_repair_return.return_approval_state.ToString()}不允许修改,无法重复提交";
                            return result;
                        }
                    }
                }
                ///如果临时保存时没有保存该值 则在这里给一个格式化的数据
                if (model.return_pic == null)
                {
                    model.return_pic = new List<string>();
                }
                //默认数据都合法了,开始保存到数据库 
                customer_repair_return new_mode = new customer_repair_return()
                {
                    add_time = DateTime.Now,
                    repair_id = model.repair_id,
                    return_type = model.return_type.Value,
                    return_express_no = model.return_express_no,
                    return_des = model.return_des,
                    return_pic = Newtonsoft.Json.JsonConvert.SerializeObject(model.return_pic),
                    submit_uid = _tool.di_user.USER.uid,
                    submit_uname = _tool.di_user.USER.u_name,
                    update_time = DateTime.Now
                };
                if (string.IsNullOrWhiteSpace(model.repair_return_id))
                {
                    //获取当前最高提交了几次审批 从0开始  然后添加数据+1
                    var max_sub_sort = _tool.di_db.GetList<int>("select isnull(max(return_sort),0) from customer_repair_return where repair_id=@repair_id", new { model.repair_id }).FirstOrDefault();

                    //当前新提交了一次审核
                    new_mode.repair_return_id = _tool.di_getid.IGet_SnowFlake_Id;
                    new_mode.is_last = true;//新提交的数据一定是最后一个,那么在添加数据前,需要先将这次报修之前的最终状态变为false
                    new_mode.return_sort = max_sub_sort + 1;
                    if (!model.temp_save.Value)
                    {
                        //如果当前是正式提交的话,需要将可以更新变更为false 也就是说提交上去了就不允许再重新提交了,除非有审核结果了
                        new_mode.can_update = false;
                        new_mode.return_approval_state = return_approval_state.返件审批已提交;
                    }
                    else
                    {
                        new_mode.can_update = true;
                        new_mode.return_approval_state = return_approval_state.保存未提交;
                    }
                    string error = "";
                    int tranrows = _tool.di_db.StartTran((conn, tran) =>
                    {
                        int rows = 0;
                        if (!model.temp_save.Value)
                        {
                            //只有正式提交需要作出该修改
                            //因为要新增数据 所以需要将之前数据库里的最终数据变更为false 标记他们不再最终的数据了
                            rows += conn.Execute("update customer_repair_return set is_last=@is_last where repair_id=@repair_id", new { model.repair_id, is_last = false }, tran);
                            //只要正式提交了一次审批,那么就直接更新一下主线数据
                            rows += conn.Execute("update customer_repair set reback_state=@reback_state where repair_id=@repair_id", new { model.repair_id, reback_state = return_approval_state.返件审批已提交 }, tran);
                        }
                        rows += conn.Execute(_tool.di_db.AddBySqlStr<customer_repair_return>(), new_mode, tran);
                        return rows;
                    }, ref error);
                    if (string.IsNullOrWhiteSpace(error))
                    {
                        result.code = 0;
                        result.msg = "提交成功";
                    }
                    else
                    {
                        result.code = 1;
                        result.msg = "提交返件审核失败:" + error;
                    }
                }
                else
                {
                    if (check_customer_repair_return == null)
                    {
                        result.code = 1;
                        result.msg = "修改返件审核不存在";
                        return result;
                    }
                    new_mode.repair_return_id = check_customer_repair_return.repair_return_id;
                    new_mode.is_last = true;
                    //开始做一个填空题
                    if (!model.temp_save.Value)
                    {
                        //如果当前是正式提交的话,需要将可以更新变更为false 也就是说提交上去了就不允许再重新提交了,除非有审核结果了
                        new_mode.can_update = false;
                        new_mode.return_approval_state = return_approval_state.返件审批已提交;
                    }
                    else
                    {
                        new_mode.can_update = true;
                        new_mode.return_approval_state = return_approval_state.保存未提交;
                    }
                    //开始保存更新
                    string error = "";
                    int tranrows = _tool.di_db.StartTran((conn, tran) =>
                    {
                        int rows = 0;
                        if (!model.temp_save.Value)
                        {
                            //只有正式提交需要作出该修改 只要是能提交到这一步的数据 我都认为可以是最后一条了
                            rows += conn.Execute("update customer_repair_return set is_last=@is_last where repair_id=@repair_id", new { model.repair_id, is_last = false }, tran);
                            //更新的时候 如果提出了正式提交 那么也更新一下主线数据
                            rows += conn.Execute("update customer_repair set reback_state=@reback_state where repair_id=@repair_id", new { model.repair_id, reback_state = return_approval_state.返件审批已提交 }, tran);
                        }
                        rows += conn.Execute(_tool.di_db.UpdateBySqlstr<customer_repair_return>(new string[] {
                            "return_type","return_express_no","return_des","return_pic","submit_uid","submit_uname","update_time","can_update","is_last","return_approval_state"
                        }, new string[] { "repair_return_id=@repair_return_id" }), new_mode, tran);
                        return rows;
                    }, ref error);
                    if (string.IsNullOrWhiteSpace(error))
                    {
                        result.code = 0;
                        result.msg = "提交成功";
                    }
                    else
                    {
                        result.code = 1;
                        result.msg = "提交返件审核失败:" + error;
                    }
                }
                return result;
            });
        }
        /// <summary>
        /// 返回返件审核列表 
        /// </summary>
        /// <returns></returns>
        public ResultObjModel get_return_approval(string repair_id)
        {
            return CoreStartDo.ResultAction(result =>
            {
                if (string.IsNullOrWhiteSpace(repair_id))
                {
                    result.code = 1;
                    result.msg = "报修ID不能为空";
                    return result;
                }
                var checkrepair = _tool.di_db.GetModel<customer_repair>(new string[] { "repair_id=@repair_id" }, new { repair_id });
                if (checkrepair == null)
                {
                    result.code = 1;
                    result.msg = "报修信息不存在";
                    return result;
                }
                //获取指定报修返件列表
                var return_list = _tool.di_db.GetList<customer_repair_return>("select * from customer_repair_return where repair_id=@repair_id", new
                {
                    repair_id
                });
                if (_tool.di_user.USER.role_id == "Approval" || _tool.di_user.USER.role_id == "Finance_Manager")
                {
                    //return_list = return_list.Where(s => s.return_approval_state >= return_approval_state.返件审批已提交);
                }
                var result_list = from o in return_list.OrderBy(s => s.return_sort)
                                  select new
                                  {
                                      o.repair_return_id, //返件ID
                                      o.repair_id, //编辑时间
                                      o.add_time, //添加时间
                                      o.return_type,//返件类型
                                      return_type_str = o.return_type.ToString(), //返件类型文字描述
                                      o.return_express_no, //返件单号
                                      o.return_des, //返件描述
                                      o.return_approval_state,///审核状态
                                      return_approval_state_str = o.return_approval_state.ToString(),//审核状态
                                      o.return_approval_des, //审核描述
                                      o.return_approval_name, ///审核人
                                      o.approval_time, //审核时间,
                                      o.can_update, //是否可以更新 如果返回列表内一条可以更新的数据都没有 那就只能做添加了
                                      o.return_pic,
                                      return_pic_array = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(o.return_pic),
                                      o.is_last,
                                      is_enable = (o.is_last && o.return_approval_state == return_approval_state.返件审批已提交) ? true : false
                                  }; 
                result.code = 0;
                result.msg = "success";
                result.data = new
                {
                    sn = checkrepair.mch_sn,
                    checkrepair.express_no,
                    checkrepair.repair_des,
                    no_can_update_return_list = result_list.Where(s=> !s.can_update),
                    can_update_return_list = result_list.Where(s => s.can_update).FirstOrDefault(),
                };

                return result;
            });
        }


        /// <summary>
        /// 处理返件状态
        /// </summary>
        /// <param name="repair_return_id">返件ID</param>
        /// <param name="state">返件状态</param>
        /// <param name="return_approval_des">返件描述</param>
        /// <param name="open_add">是否开启再次返件</param>
        /// <returns></returns>
        public ResultModel handle_apply_return_state(string repair_return_id, return_approval_state? state, string return_approval_des, bool? open_add = false)
        {
            return CoreStartDo.ResultAction<ResultModel>(result =>
            {
                if (_tool.di_user.USER.role_id != "Approval" && _tool.di_user.USER.role_id != "Finance_Manager")
                {
                    result.code = 1;
                    result.msg = "只有审核人才可以进行该操作";
                    return result;
                }
                if (string.IsNullOrWhiteSpace(repair_return_id))
                {
                    result.code = 1;
                    result.msg = "参数异常,id未入参";
                    return result;
                }
                if (!state.HasValue)
                {
                    result.code = 1;
                    result.msg = "必须入参审核状态";
                    return result;
                }
                //获取返回审核数据
                var return_model = _tool.di_db.GetList<customer_repair_return>("select * from customer_repair_return where repair_return_id=@repair_return_id", new
                {
                    repair_return_id
                }).FirstOrDefault();
                if (return_model == null)
                {
                    result.code = 1;
                    result.msg = "报修返件审核不存在";
                    return result;
                }
                if (!new return_approval_state[] { return_approval_state.保存未提交, return_approval_state.返件审批已提交, return_approval_state.返件审批被驳回 }.Contains(return_model.return_approval_state))
                {
                    result.code = 1;
                    result.msg = $"当前返件状态{return_model.return_approval_state.ToString()} 无法修改审核状态";
                    return result;
                }
                var checkrepair = _tool.di_db.GetModel<customer_repair>(new string[] { "repair_id=@repair_id" }, new { return_model.repair_id });
                if (checkrepair == null)
                {
                    result.code = 1;
                    result.msg = "报修信息不存在";
                    return result;
                }
                bool can_update = return_model.can_update;
                if (state == return_approval_state.返件审批被驳回 && !open_add.Value)
                {
                    //只有设置返件被驳回并且没有开启再次返件时才可以进行修改
                    can_update = true;
                }
                else
                {
                    can_update = false;
                }
                DateTime? approval_time = return_model.approval_time;
                if (state == return_approval_state.返件审批已完成)
                {
                    approval_time = DateTime.Now;//如果标记处理完成 则需要标记时间
                }
                string error_msg = "";
                int tran_rows = _tool.di_db.StartTran((conn, tran) =>
                {
                    int rows = 0;
                    rows += conn.Execute(_tool.di_db.UpdateBySqlstr<customer_repair_return>(new string[] {
                        "return_approval_state",
                        "return_approval_des",
                        "can_update",
                        "return_approval_uid",
                        "approval_time",
                        "return_approval_name"
                    }, new string[] {
                        "repair_return_id=@repair_return_id"
                    }), new
                    {
                        return_approval_state = state,
                        return_approval_des,
                        return_approval_uid = _tool.di_user.USER.uid,
                        return_approval_name = _tool.di_user.USER.u_name,
                        approval_time,
                        repair_return_id,
                        can_update
                    }, tran);
                    //更新主表数据状态 
                    rows += conn.Execute("update customer_repair set reback_state=@reback_state,reback_result=@reback_result,reback_time=@reback_time where repair_id=@repair_id", new
                    {
                        return_model.repair_id,
                        reback_state = state,
                        reback_result = return_approval_des,
                        reback_time = approval_time
                    }, tran);
                    return rows;
                }, ref error_msg);
                if (tran_rows > 0)
                {
                    result.code = 0;
                    result.msg = "审核成功";
                }
                else
                {
                    result.code = 1;
                    result.msg = "审核失败: " + error_msg;
                }
                return result;
            });
        }



        /// <summary>
        /// 获取返件列表
        /// </summary>
        /// <returns></returns>
        public ResultListModel<return_approval_item> get_return_approval_list(search_return_approval_list query)
        {
            return CoreStartDo.ResultAction<ResultListModel<return_approval_item>>(result =>
            {
                string sql = "select a.*,b.repair_time,b.customer_uname,b.user_name from customer_repair_return a left join  customer_repair b on a.repair_id=b.repair_id";
                string where = " where a.is_last=@is_last and a.return_approval_state!=@return_approval_state";
                dynamic query_dy = new System.Dynamic.ExpandoObject();
                query_dy.is_last = true;
                query_dy.return_approval_state = return_approval_state.保存未提交;
                if (!string.IsNullOrWhiteSpace(query.customer_name))
                {
                    where += " and b.user_name like @user_name";
                    query_dy.user_name = $"%{query.customer_name}%";
                }
                if (!string.IsNullOrWhiteSpace(query.submit_uid))
                {
                    where += " and a.submit_uid=@submit_uid";
                    query_dy.submit_uid = query.submit_uid;
                }
                if (query.return_type.HasValue)
                {
                    where += " and a.return_type=@return_type";
                    query_dy.return_type = query.return_type.Value;
                }
                if (query.return_approval_state.HasValue)
                {
                    where += " and a.return_approval_state=@return_approval_state";
                    query_dy.return_approval_state = query.return_approval_state.Value;
                }
                if (!string.IsNullOrWhiteSpace(query.approval_uid))
                {
                    where += " and a.return_approval_uid=@approval_uid";
                    query_dy.approval_uid = query.approval_uid;
                }
                if (query.submit_count.HasValue)
                {
                    where += " and a.return_sort=@return_sort";
                    query_dy.return_sort = query.submit_count;
                }
                if (!string.IsNullOrWhiteSpace(query.return_express_no))
                {
                    where += " and a.return_express_no=@return_express_no";
                    query_dy.return_express_no = query.return_express_no;
                }
                if (!string.IsNullOrWhiteSpace(query.start_addtime))
                {
                    DateTime start_addtime_t;
                    if (DateTime.TryParse(query.start_addtime, out start_addtime_t))
                    {
                        where += " and a.add_time>=@start_addtime_t";
                        query_dy.start_addtime_t = start_addtime_t;
                    }
                }
                if (!string.IsNullOrWhiteSpace(query.end_addtime))
                {
                    DateTime end_addtime_t;
                    if (DateTime.TryParse(query.end_addtime, out end_addtime_t))
                    {
                        where += " and a.add_time<=@end_addtime_t";
                        query_dy.end_addtime_t = end_addtime_t;
                    }
                }
                if (!string.IsNullOrWhiteSpace(query.start_approval_time))
                {
                    DateTime start_addtime_approval;
                    if (DateTime.TryParse(query.start_approval_time, out start_addtime_approval))
                    {
                        where += " and a.approval_time>=@start_addtime_approval";
                        query_dy.start_addtime_approval = start_addtime_approval;
                    }
                }
                if (!string.IsNullOrWhiteSpace(query.end_approval_time))
                {
                    DateTime end_addtime_approval;
                    if (DateTime.TryParse(query.end_approval_time, out end_addtime_approval))
                    {
                        where += " and a.approval_time<=@end_addtime_t";
                        query_dy.end_addtime_t = end_addtime_approval;
                    }
                }

                ResultListModel<return_approval_item> result_list = _tool.di_db.GetPageList<return_approval_item>(sql, where, "a.add_time", query_dy, query.IsPage, query.page, query.pagesize);
                result = result_list;
                return result;
            });
        }

         
        /// <summary>
        /// 针对张欢需要补充系统上线之前的返件记录情况 开一个后门
        /// </summary>
        /// <returns></returns>
        public ResultObjModel submit_return_approval_for_zhanghuan(submit_return_approval_params model)
        {
            return CoreStartDo.ResultAction(result =>
            {
                if (_tool.di_user.USER.uid != "1258327306334781440")
                {
                    result.code = 1;
                    result.msg = "您可能不是张欢";  // = = 行吧
                    return result;
                }
                if (string.IsNullOrWhiteSpace(model.repair_id))
                {
                    result.code = 1;
                    result.msg = "报修ID不能为空";
                    return result;
                }
                if (!model.return_type.HasValue)
                {
                    result.code = 1;
                    result.msg = "返件类型不能为空";
                    return result;
                }
                if (!model.temp_save.HasValue)
                {
                    result.code = 1;
                    result.msg = "必须告知储存模式";
                    return result;
                }
                var checkrepair = _tool.di_db.GetModel<customer_repair>(new string[] { "repair_id=@repair_id" }, new { model.repair_id });
                if (checkrepair == null)
                {
                    result.code = 1;
                    result.msg = "报修信息不存在";
                    return result;
                }
                //尝试获取当前一条的
                customer_repair_return check_customer_repair_return = _tool.di_db.GetModel<customer_repair_return>(new string[] { "repair_return_id=@repair_return_id" }, new { model.repair_return_id });
                //获取已提交数据列表
                var repair_list = _tool.di_db.GetList<customer_repair_return>("select * from customer_repair_return  where repair_id=@repair_id", new { model.repair_id });
                if (!model.temp_save.Value)
                {
                    //如果当前并非临时储存,则开始进行正式判断
                    if (model.return_type.Value == return_type.同意返件)
                    {
                        if (string.IsNullOrWhiteSpace(model.return_express_no))
                        {
                            result.code = 1;
                            result.msg = "返件单号不能为空";
                            return result;
                        }
                    }
                    else if (model.return_type.Value == return_type.不同意返件)
                    {
                        if (string.IsNullOrWhiteSpace(model.return_des))
                        {
                            result.code = 1;
                            result.msg = "说明不能为空";
                            return result;
                        }
                        if (model.return_pic == null || !model.return_pic.Any())
                        {
                            result.code = 1;
                            result.msg = "至少上传一张图片";
                            return result;
                        }
                    }
                    if (repair_list.Where(s => s.return_approval_state == return_approval_state.返件审批已完成).Any())
                    {
                        result.code = 1;
                        result.msg = "当前报修审批已完成";
                        return result;
                    }
                    if (!string.IsNullOrWhiteSpace(model.repair_return_id))
                    {
                        //如果是提交的修改，则需要判断当前返件请求是否可以重复修改 
                        if (check_customer_repair_return == null)
                        {
                            result.code = 1;
                            result.msg = "修改提交的返件处理不存在";
                            return result;
                        }
                        if (!check_customer_repair_return.is_last)
                        {
                            result.code = 1;
                            result.msg = "非法操作:无法重复提交已关闭的审核";
                            return result;
                        }
                        if (!check_customer_repair_return.can_update)
                        {
                            result.code = 1;
                            result.msg = $"非法操作:当前审核状态{check_customer_repair_return.return_approval_state.ToString()}不允许修改,无法重复提交";
                            return result;
                        }
                    }
                    else
                    {
                        //当前添加审批,需要判断针对此次报修提交次数是否超过3次
                        var sub_count = repair_list.Count();//_tool.di_db.GetList<int>("select count(*) from customer_repair_return  where repair_id=@repair_id", new { model.repair_id }).FirstOrDefault();
                        if (sub_count == 3)
                        {
                            //当前已经有3次提交数据了,不允许再进行提交
                            result.code = 1;
                            result.msg = "当次报修已提交了三次返件审核，无法继续提交";
                            return result;
                        }
                        var can_update_rows = repair_list.Where(s => s.can_update == true).Count();//_tool.di_db.GetList<int>("select count(*) from customer_repair_return where repair_id=@repair_id and can_update=@can_update", new { model.repair_id, can_update = true }).FirstOrDefault();
                        if (can_update_rows > 0)
                        {
                            result.code = 1;
                            result.msg = "非法操作:当前存在可以修改的返件提交,无法提交新的申请";
                            return result;
                        }
                        var add_sub_count = repair_list.Where(s => new return_approval_state[] {
                            return_approval_state.保存未提交 , return_approval_state.返件审批已提交}.Contains(s.return_approval_state)).Count(); //_tool.di_db.GetList<int>("select count(*) from customer_repair_return  where repair_id=@repair_id and return_approval_state=@return_approval_state", new { model.repair_id, return_approval_state = return_approval_state.保存未提交 }).FirstOrDefault();
                        if (add_sub_count > 0)
                        {
                            result.code = 1;
                            result.msg = "已存在临时保存的提交, 请确认状态后再重新提交";
                            return result;
                        }
                    }
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(model.repair_return_id))
                    {
                        //如果是添加数据，则需要判断是否还有其他临时保存的数据
                        //如果当前是临时保存的数据 则需要判断，是否存在同样临时保存的数据
                        var sub_count = repair_list.Where(s => new return_approval_state[] {
                            return_approval_state.保存未提交 , return_approval_state.返件审批已提交}.Contains(s.return_approval_state)).Count();  //_tool.di_db.GetList<int>("select count(*) from customer_repair_return  where repair_id=@repair_id and return_approval_state=@return_approval_state", new { model.repair_id, return_approval_state = return_approval_state.保存未提交 }).FirstOrDefault();
                        if (sub_count > 0)
                        {
                            result.code = 1;
                            result.msg = "已存在临时保存的提交, 请确认状态后再重新提交";
                            return result;
                        }
                    }
                    else
                    {
                        if (!check_customer_repair_return.can_update)
                        {
                            result.code = 1;
                            result.msg = $"非法操作:当前审核状态{check_customer_repair_return.return_approval_state.ToString()}不允许修改,无法重复提交";
                            return result;
                        }
                    }
                }
                ///如果临时保存时没有保存该值 则在这里给一个格式化的数据
                if (model.return_pic == null)
                {
                    model.return_pic = new List<string>();
                }
                //默认数据都合法了,开始保存到数据库 
                customer_repair_return new_mode = new customer_repair_return()
                {
                    add_time = DateTime.Now,
                    repair_id = model.repair_id,
                    return_type = model.return_type.Value,
                    return_express_no = model.return_express_no,
                    return_des = model.return_des,
                    return_pic = Newtonsoft.Json.JsonConvert.SerializeObject(model.return_pic),
                    submit_uid = _tool.di_user.USER.uid,
                    submit_uname = _tool.di_user.USER.u_name,
                    update_time = DateTime.Now
                };
                if (string.IsNullOrWhiteSpace(model.repair_return_id))
                {
                    //获取当前最高提交了几次审批 从0开始  然后添加数据+1
                    var max_sub_sort = _tool.di_db.GetList<int>("select isnull(max(return_sort),0) from customer_repair_return where repair_id=@repair_id", new { model.repair_id }).FirstOrDefault();

                    //当前新提交了一次审核
                    new_mode.repair_return_id = "zhhb_" + _tool.di_getid.IGet_SnowFlake_Id;
                    new_mode.is_last = true;//新提交的数据一定是最后一个,那么在添加数据前,需要先将这次报修之前的最终状态变为false
                    new_mode.return_sort = max_sub_sort + 1;
                    if (!model.temp_save.Value)
                    {
                        //如果当前是正式提交的话,需要将可以更新变更为false 也就是说提交上去了就不允许再重新提交了,除非有审核结果了
                        new_mode.can_update = false;
                        new_mode.return_approval_state = return_approval_state.返件审批已完成;//调用该接口直接变成已完成
                    }
                    else
                    {
                        new_mode.can_update = true;
                        new_mode.return_approval_state = return_approval_state.保存未提交;
                    }
                    string error = "";
                    int tranrows = _tool.di_db.StartTran((conn, tran) =>
                    {
                        int rows = 0;
                        if (!model.temp_save.Value)
                        {
                            //只有正式提交需要作出该修改
                            //因为要新增数据 所以需要将之前数据库里的最终数据变更为false 标记他们不再最终的数据了
                            rows += conn.Execute("update customer_repair_return set is_last=@is_last where repair_id=@repair_id", new { model.repair_id, is_last = false }, tran);
                            //只要正式提交了一次审批,那么就直接更新一下主线数据
                            rows += conn.Execute("update customer_repair set reback_state=@reback_state where repair_id=@repair_id", new { model.repair_id, reback_state = return_approval_state.返件审批已完成 }, tran);
                        }
                        rows += conn.Execute(_tool.di_db.AddBySqlStr<customer_repair_return>(), new_mode, tran);
                        return rows;
                    }, ref error);
                    if (string.IsNullOrWhiteSpace(error))
                    {
                        result.code = 0;
                        result.msg = "提交成功";
                    }
                    else
                    {
                        result.code = 1;
                        result.msg = "提交返件审核失败:" + error;
                    }
                }
                else
                {
                    if (check_customer_repair_return == null)
                    {
                        result.code = 1;
                        result.msg = "修改返件审核不存在";
                        return result;
                    }
                    if(!check_customer_repair_return.repair_return_id.Contains("zhhb_"))
                    {
                        result.code = 1;
                        result.msg = "试图修改非补充返件数据 error";
                        return result;
                    }
                    new_mode.repair_return_id = check_customer_repair_return.repair_return_id;
                    new_mode.is_last = true;
                    //开始做一个填空题
                    if (!model.temp_save.Value)
                    {
                        //如果当前是正式提交的话,需要将可以更新变更为false 也就是说提交上去了就不允许再重新提交了,除非有审核结果了
                        new_mode.can_update = false;
                        new_mode.return_approval_state = return_approval_state.返件审批已完成;
                    }
                    else
                    {
                        new_mode.can_update = true;
                        new_mode.return_approval_state = return_approval_state.保存未提交;
                    }
                    //开始保存更新
                    string error = "";
                    int tranrows = _tool.di_db.StartTran((conn, tran) =>
                    {
                        int rows = 0;
                        if (!model.temp_save.Value)
                        {
                            //只有正式提交需要作出该修改 只要是能提交到这一步的数据 我都认为可以是最后一条了
                            rows += conn.Execute("update customer_repair_return set is_last=@is_last where repair_id=@repair_id", new { model.repair_id, is_last = false }, tran);
                            //更新的时候 如果提出了正式提交 那么也更新一下主线数据
                            rows += conn.Execute("update customer_repair set reback_state=@reback_state where repair_id=@repair_id", new { model.repair_id, reback_state = return_approval_state.返件审批已完成 }, tran);
                        }
                        rows += conn.Execute(_tool.di_db.UpdateBySqlstr<customer_repair_return>(new string[] {
                            "return_type","return_express_no","return_des","return_pic","submit_uid","submit_uname","update_time","can_update","is_last","return_approval_state"
                        }, new string[] { "repair_return_id=@repair_return_id" }), new_mode, tran);
                        return rows;
                    }, ref error);
                    if (string.IsNullOrWhiteSpace(error))
                    {
                        result.code = 0;
                        result.msg = "提交成功";
                    }
                    else
                    {
                        result.code = 1;
                        result.msg = "提交返件审核失败:" + error;
                    }
                }
                return result;
            });
        }
        #endregion

        #region 2020-09-07 针对超保审批新增一个给审批人看的列表

        /// <summary>
        ///  根据搜索条件获取登录人的报修信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public ResultListModel get_over_insurance_approval_pagelist(search_my_approval_list query)
        {
            return CoreStartDo.ResultAction<ResultListModel>(result =>
            {
                string sql = "select * from customer_repair";
                string where = " where approval_model=@approval_model";
                where += " and repair_approval_addtime>=@addtime_sdt and repair_approval_addtime<=@addtime_edt";
                dynamic query_dy = new System.Dynamic.ExpandoObject();
                query_dy.approval_model = approval_model.超保审批;
                //提交时间查询
                if (!string.IsNullOrWhiteSpace(query.addtime_sdt))
                {
                    query_dy.addtime_sdt = query.addtime_sdt;
                }
                else
                {
                    query_dy.addtime_sdt = DateTime.Now.AddYears(-1);
                }
                //提交时间查询
                if (!string.IsNullOrWhiteSpace(query.addtime_edt))
                {
                    query_dy.addtime_edt = query.addtime_edt;
                }
                else
                {
                    query_dy.addtime_edt = DateTime.Now;
                }
                if (!string.IsNullOrWhiteSpace(query.user_name))
                {
                    where += " and user_name like @user_name";
                    query_dy.user_name = $"%{query.user_name}%";
                }
                //技术售后查询
                if (!string.IsNullOrWhiteSpace(query.technology_uid))
                {
                    where += " and technology_uid=@technology_uid";
                    query_dy.technology_uid = query.technology_uid;
                }
                //审批状态查询
                if (query.repair_handle_state.HasValue)
                {
                    where += " and repair_handle_state=@repair_handle_state";
                    query_dy.repair_handle_state = query.repair_handle_state.Value;
                }
                //审核时间查询
                if (!string.IsNullOrWhiteSpace(query.repair_approval_time_sdt))
                {
                    where += " and repair_approval_time>=@repair_approval_time_sdt";
                    query_dy.repair_approval_time_sdt = query.repair_approval_time_sdt;
                }
                //审核时间查询
                if (!string.IsNullOrWhiteSpace(query.repair_approval_time_edt))
                {
                    where += " and repair_approval_time<=@repair_approval_time_edt";
                    query_dy.repair_approval_time_edt = query.repair_approval_time_edt;
                }
                if (query.have_pic.HasValue)
                {
                    if (query.have_pic.Value)
                    {
                        //查询有图片的
                        where += " and repair_express_no is not null and repair_express_no<>''";
                    }
                    else
                    {
                        //查询没图片的
                        where += " and (repair_express_no is null or repair_express_no='')";
                    }
                }
                if (query.reback_state.HasValue)
                {
                    where += " and reback_state=@reback_state";
                    query_dy.reback_state = query.reback_state;
                }
                if (!string.IsNullOrWhiteSpace(query.express_no))
                {
                    where += " and express_no=@express_no";
                    query_dy.express_no = query.express_no;
                }
                if (query.express_state.HasValue)
                {
                    if (query.express_state.Value == express_state.无寄件)
                    {
                        where += " and (express_state is null or express_state=@express_state)";
                    }
                    else
                    {
                        where += " and express_state=@express_state";
                    }
                    query_dy.express_state = query.express_state.Value;
                }
                ResultListModel<customer_repair> result_pagelist = _tool.di_db.GetPageList<customer_repair>(sql, where, "addtime", query_dy, query.IsPage, query.page, query.pagesize);
                var q = from o in result_pagelist.rows
                        select new
                        {
                            o.repair_id,//报修ID
                            o.addtime,//报修日期, 
                            o.user_name,//客户名称, 
                            o.mch_assurance_sdt,//质保开始时间
                            o.technology_uname,//提交人, 
                            o.repair_approval_addtime,//提交日期 
                            o.approval_name,//审批人, 
                            repair_handle_state_str = ((repair_handle_state)o.repair_handle_state).ToString(),
                            o.repair_approval_time,//审批时间
                            o.repair_express_no,
                            o.repair_content_pic,
                            hava_pic = string.IsNullOrWhiteSpace(o.repair_express_no) ? false : true,
                            o.repair_approval_des,
                            o.repair_des_pic,
                            o.repair_handle_state,
                            o.approval_price, //审批金额
                            o.freight,//运费
                            o.express_model,//快递模式, 
                            o.approval_model,//审核模式
                            o.reback_state,
                            o.reback_time,
                            o.reback_result,
                            o.reback_confirm_uid,
                            o.express_no,
                            o.express_state,
                            express_state_str = o.express_state.HasValue ? o.express_state.ToString() : express_state.无寄件.ToString(),
                            reback_state_str = o.reback_state.ToString(),
                            o.transfer_screenshot
                        };
                result.code = 0;
                result.msg = "success";
                result.rows = q.ToList();
                result.total = result_pagelist.total;
                result.current = query.page.Value;
                return result;

            });
        }
        #endregion 
    }
}
